[英]Insert database using MS Access in C#
It doesn't have an error, but it has a message box showing Ms Access Database Engine and data not insert to database. 它没有错误,但是有一个消息框,显示Ms Access数据库引擎,并且数据未插入数据库。 Can anyone help me solve the problem??
谁能帮我解决问题?
namespace WindowsFormsApplication1
{
public partial class SignUp : Form
{
public SignUp()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Junz\Documents\Register - Copy.mdb";
conn.Open();
String Username = textBox1.Text;
String Password = textBox2.Text;
String Email = textBox3.Text;
String Address = textBox4.Text;
OleDbCommand cmd = new OleDbCommand("INSERT INTO Register(Username,Password,Email,Address) Values(@Username, @Password,@Email,@Address)");
cmd.Connection = conn;
if (conn.State == ConnectionState.Open)
{
cmd.Parameters.Add("@Username", OleDbType.VarChar,20).Value = Username;
cmd.Parameters.Add("@Password", OleDbType.VarChar,20).Value = Password;
cmd.Parameters.Add("@Email", OleDbType.VarChar,20).Value = Email;
cmd.Parameters.Add("@Address", OleDbType.VarChar,20).Value = Address;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("DATA ADDED");
conn.Close();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Source);
conn.Close();
}
}
else
{
MessageBox.Show("Connection Failed");
}
}
}
}
ExecuteNonQuery()
will return int
value representing number of rows updated into Database
. ExecuteNonQuery()
将返回int
值,该值表示更新到Database
的行数。
if the returned value is 0
you can Display a Message saying DATA NOT ADDED
如果返回值为
0
,则可以显示一条消息,提示未DATA NOT ADDED
Try This: write your try
block as below. 试试看:编写如下的
try
块。
try
{
if(cmd.ExecuteNonQuery()>0)
MessageBox.Show("DATA ADDED");
else
MessageBox.Show("DATA NOT ADDED");
conn.Close();
}
how about this hope it helps 希望有什么帮助
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Junz\Documents\Register - Copy.mdb";
conn.Open();
String Username = textBox1.Text;
String Password = textBox2.Text;
String Email = textBox3.Text;
String Address = textBox4.Text;
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Register(Username,Password,Email,Address) Values(@Username, @Password,@Email,@Address)";
if (conn.State == ConnectionState.Open)
{
cmd.Parameters.Add("@Username", OleDbType.VarChar,20).Value = Username;
cmd.Parameters.Add("@Password", OleDbType.VarChar,20).Value = Password;
cmd.Parameters.Add("@Email", OleDbType.VarChar,20).Value = Email;
cmd.Parameters.Add("@Address", OleDbType.VarChar,20).Value = Address;
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("DATA ADDED");
conn.Close();
}
catch (OleDbException ex)
{
MessageBox.Show(ex.Source);
conn.Close();
}
}
else
{
MessageBox.Show("Connection Failed");
}
}
Add this to your catch 将此添加到您的捕获
catch (OleDbException ex){
MessageBox.Show(ex.Message);
conn.Close();
}
And try to insert again, maybe u can see the exact error now. 并尝试再次插入,也许您现在可以看到确切的错误。
Good luck. 祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.