[英]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.. 我什至尝试刷新数据库。
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文件来获取连接字符串。
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.