简体   繁体   English

记录已成功插入,但数据库未更新C#和SQL Server Compact数据库

[英]Record inserted successfully but database not updated C# and SQL Server Compact Database

private void button1_Click(object sender, EventArgs e)
{
        string usernames = textBox1.Text;
        string passwords = textBox2.Text;
        string emailid = textBox5.Text;
        string telno = textBox6.Text;

        string connectionstring = "Data Source=|DataDirectory|\\libdb.sdf; Persist Security Info=False ;";
        using (SqlCeConnection con = new SqlCeConnection(connectionstring))
        {
            con.Open();

            using (SqlCeCommand Query = new SqlCeCommand("INSERT INTO Registers " + "(usernames,passwords,emailid,telno) " + "VALUES (@usernames,@passwords,@emailid,@telno)", con))
            {

                Query.Parameters.AddWithValue("@usernames", usernames);
                Query.Parameters.AddWithValue("@passwords", passwords);
                Query.Parameters.AddWithValue("@emailid", emailid);
                Query.Parameters.AddWithValue("@telno", telno);
                Query.ExecuteNonQuery();
            }
            MessageBox.Show("QueryExecuted");
            con.Close();
            MessageBox.Show("Closedconnecrion");
            con.Dispose();
            MessageBox.Show("disposed");
            this.Close();

            /*string conString = "Data Source=" + 
            Path.Combine(
                   Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                   "MyAppData\\database.sdf") + ";Password=yourPassword;";
             even  this method dosent works */
        }

    }
}

on executing this code I find it executes successfully. 执行此代码后,我发现它执行成功。 But when I go and check the database, I find the entries empty... 但是当我检查数据库时,发现条目为空...

I even tried refreshing database.. 我什至尝试刷新数据库。

  1. I didn't find problem in connectivity. 我没有发现连接方面的问题。
  2. Query no error executed successfully. 查询无错误,执行成功。

The problem is I didn't find result or the data I gave as input in database. 问题是我没有找到结果或我作为数据库输入提供的数据。

Please be descriptive with code eg and mail to scarlet.gabriel@gmail.com 请使用代码进行描述,例如,然后发送至Scarlet.gabriel@gmail.com

Test your connection with the database first, You can get the connection string by creating the udl file. 首先测试与数据库的连接,您可以通过创建udl文件来获取连接字符串。

  1. create a textfile and savaas it with the extension udl (example connection.udl). 创建一个文本文件,并使用扩展名udl(例如connection.udl)对其进行保存。
  2. double click and run this file. 双击并运行该文件。
  3. a window will be opened where you can get the option for getting the connection string. 将打开一个窗口,您可以在其中获取用于获取连接字符串的选项。
  4. after testing the connection, close this window and open this file with a note pad. 测试连接后,关闭此窗口并使用记事本打开此文件。
  5. copy the connection string and paste it in the below statement. 复制连接字符串并将其粘贴在以下语句中。

      string connectionstring =" paste here"; 

How can you be sure if the connection is open and has done the job? 您如何确定连接是否打开并已完成工作? Use try... catch block to catch if any error occurs: 使用try ... catch块来捕获是否发生任何错误:

using (SqlCeConnection con = new SqlCeConnection(connectionstring))
    {
       try{con.Open();}
       catch(Exception ex)
       {
         // database connection error. log/display ex. > return.
       }

       if(con.State==ConnectionState.Open)
       { 
         using (SqlCeCommand Query = new SqlCeCommand("INSERT INTO Registers " + "(usernames,passwords,emailid,telno) " + "VALUES (@usernames,@passwords,@emailid,@telno)", con))
         {

            Query.Parameters.AddWithValue("@usernames", usernames);
            Query.Parameters.AddWithValue("@passwords", passwords);
            Query.Parameters.AddWithValue("@emailid", emailid);
            Query.Parameters.AddWithValue("@telno", telno);
            try{
                Query.ExecuteNonQuery();
               }
            catch(Exception ex)
               {
                  // database communication error. log/display ex
               }
         }
           MessageBox.Show("QueryExecuted");
        }
        if(con.State==ConnectionState.Open)
        { 
           try{con.Close();}
           catch{}
         }

    }

Instead of using connectionstring = "Data Source=|DataDirectory|\\libdb.sdf; Persist Security Info=False ;"; 而不是使用connectionstring =“ Data Source = | DataDirectory | \\ libdb.sdf; Persist Security Info = False;”; Please try to use absolute path like connectionstring = "Data Source=C:\\Users\\chandra\\Documents\\Visual Studio 2010\\Projects\\Window\\LMS\\AppData\\LMSDatabase.sdf;Persist Security Info=False;"; 请尝试使用绝对路径,例如connectionstring =“ Data Source = C:\\ Users \\ chandra \\ Documents \\ Visual Studio 2010 \\ Projects \\ Window \\ LMS \\ AppData \\ LMSDatabase.sdf; Persist Security Info = False;”; I hope this will work. 我希望这会起作用。

Thanks 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#SQL Server Compact数据库登录 - C# SQL Server Compact Database Login 将C#连接到SQL Server Compact数据库 - Connecting C# to SQL Server Compact database 同步SQL Server Compact数据库以在C#中与在线数据库一起使用? - Synchronising SQL Server Compact database to work with an online database in C#? 将记录插入到SQL Server CE数据库移动到另一个线程? C#Compact Framework 3.5 - Move record insert to SQL Server CE database into another thread? C# Compact Framework 3.5 努力将记录从我的C#WinForm应用程序插入“ SQL Server Compact 4.0数据库”。 (我正在使用VS 2010) - Struggling to insert record into “SQL Server Compact 4.0 Database” from my C# WinForm Application. (I'm using VS 2010)) 使用sdf数据库(SQL Server Compact 3.5数据库文件)而不安装Microsoft sql server C# - Use sdf database(SQL Server Compact 3.5 database file) without installing Microsoft sql server C# 插入查询运行成功,但数据没有插入数据库(C#) - Insert query is run successfully, but the data is not inserted into the database (C#) 如何在C#中使用SQL Server Compact将数据库架构复制到其他数据库? - Howto copy database schema to enother database using SQL Server Compact in C#? 使用TableAdapter将记录插入SQL Server Compact数据库 - Inserting a record into SQL Server Compact database using TableAdapter C#,SQL Server Compact-优化插入记录速度 - C#, SQL Server Compact - optimize insert record speed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM