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 */
}
}
}
执行此代码时,我发现它成功执行。但是当我去检查数据库时,我发现这些条目是空的......
我甚至尝试刷新数据库..
问题是我没有找到结果或我在数据库中输入的数据。
请描述代码,例如邮寄至scarlet.gabriel@gmail.com
答案 0 :(得分:0)
首先测试您与数据库的连接,您可以通过创建udl文件来获取连接字符串。
复制连接字符串并将其粘贴到下面的语句中。
string connectionstring =" paste here";
答案 1 :(得分:0)
如何确定连接是否已打开并完成了工作?使用 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{}
}
}
答案 2 :(得分:0)
而不是使用connectionstring =“Data Source = | DataDirectory | \ libdb.sdf; Persist Security Info = False;”;请尝试使用绝对路径 connectionstring =“数据源= C:\ Users \ chandra \ Documents \ Visual Studio 2010 \ Projects \ Window \ LMS \ AppData \ LMSDatabase.sdf; Persist Security Info = False;”; 我希望这会奏效。
由于