Best practice for SQLite with WPF and ASP.NET MVC?

What is the correct way to work with SQLite in WPF and ASP.NET MVC?

  • In WPF, I have a test.sqlite file in the App_Data directory, but when I compile and run it, it does not copy the database to. / bin / Debug, so of course he cannot find this. But if I manually copy the test.sqlite file to .. / bin / Debug, then it will work. But this cannot be the way it would be possible to develop an application in which there is a local database to which he reads and writes, right? How can I mark a database as “part of an application”, so when it compiles and runs, it is copied and can be used? Or is it part of the development / publishing process? What I'm missing here, I think I have a web metaphor here that just doesn't work with a Windows application to work.

  • In ASP.MVC, I copied the same code that accesses the database in WPF, but it continues to say that it "cannot open the database" in the connection.Open () line. I have "../App_Data/test.sqlite", which, as I understand it, is the right place to access files. However, I tried to write a text file in "../App_data" and got a UnauthorizedAccessException ", so I assume that the problem is that I do not have the right to read / write here. How can I change this?

        private string getData()
    {
        StringBuilder sb = new StringBuilder();
        //string fullPathAndFileName = Server.MapPath("App_Data/testnew.sqlite");
        using (SQLiteConnection conn = 
              new SQLiteConnection(@"Data Source=" + Server.MapPath("App_Data/testnew.sqlite")))
        {
            StringBuilder query = new StringBuilder();
            query.Append("SELECT * ");
            query.Append("FROM members ");
            query.Append("ORDER BY firstName");
            using (SQLiteCommand cmd = new SQLiteCommand(query.ToString(), conn))
            {
                conn.Open();
                using (SQLiteDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        //Console.WriteLine(dr.GetValue(0) + " " + dr.GetValue(1));  
                        string id = dr.GetValue(0).ToString();
                        string firstName = dr.GetValue(1).ToString();
                        sb.Append(firstName + ", ");
    
                    }
                }
            }
        }
        return sb.ToString();
    
    }
    
+3
source share
1 answer

test.sqlite WPF, " ": " ". , , , .

UnauthorizedAccessException, App_Data ASP.Net? , , test.aspx:

<%@ Page Language="C#" %>
<script runat="server">
  protected override void OnLoad(EventArgs e)
  {
      Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
      base.OnLoad(e);
  }
</script>
+4

All Articles