C#访问数据库问题
C# access database issue
private void button1_Click(object sender, EventArgs e)
{
try
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dhelm.ALLMATINC.001\Desktop\Db11.accdb";
cmd.CommandText = @"insert into Table1 (Customer,Description,Color,Status,)VALUES('" + tBox3.Text + "','" + tBox1.Text + "','" + tBox12.Text + "','" + cBox2.Text + "')";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show("Recrod Succefully Created");
con.Close();
}
catch(Exception ex)
{ MessageBox.Show("error " + ex); }
}
现在我的数据库有
其他字段的主键是 ID,它有诸如 sqft rons peices 之类的东西。我不需要全部填写,对吗?我每次都会出错
INSERT INTO 语句中的语法错误
[![在此处输入图片描述][1]][1]
您的 SQL 似乎拼错了。正确示例:
private void button1_Click(object sender, EventArgs e)
{
try
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dhelm.ALLMATINC.001\Desktop\Db11.accdb";
cmd.CommandText = @"insert into Table1 (Customer,Description,Color,Status) VALUES('" + tBox3.Text + "','" + tBox1.Text + "','" + tBox12.Text + "','" + cBox2.Text + "')";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show("Recrod Succefully Created");
con.Close();
}
catch(Exception ex)
{
MessageBox.Show("error " + ex);
}
}
您无需在 INSERT
语句中提供所有列的值。
如果字段设置为自动递增或允许 NULL
值,则可以省略这些字段。
在您的情况下,我认为您应该将主键 ID
设置为自动递增,其他字段允许 NULL
值。
您在 "Status" 字段后多了一个“,”。您不应将值直接附加到命令,而应使用参数化值。您也可以使用 "using" -statements 自动关闭和处理连接和命令。下面是可以帮助您解决问题的小代码:
using (var con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dhelm.ALLMATINC.001\Desktop\Db11.accdb"))
{
using (var cmd = new OleDbCommand(@"
INSERT INTO
Table1(Customer,Description,Color,Status)
VALUES(@Customer,@Description,@Color,@Status)
", con))
{
cmd.Parameters.Add(new OleDbParameter("@Customer", OleDbType.VarChar)).Value = tBox3.Text;
cmd.Parameters.Add(new OleDbParameter("@Description", OleDbType.VarChar)).Value = tBox1.Text;
cmd.Parameters.Add(new OleDbParameter("@Color", OleDbType.VarChar)).Value = tBox12.Text;
cmd.Parameters.Add(new OleDbParameter("@Status", OleDbType.VarChar)).Value = cBox2.Text;
con.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show("Recrod Succefully Created");
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dhelm.ALLMATINC.001\Desktop\Db11.accdb";
cmd.CommandText = @"insert into Table1 (Customer,Description,Color,Status,)VALUES('" + tBox3.Text + "','" + tBox1.Text + "','" + tBox12.Text + "','" + cBox2.Text + "')";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show("Recrod Succefully Created");
con.Close();
}
catch(Exception ex)
{ MessageBox.Show("error " + ex); }
}
现在我的数据库有 其他字段的主键是 ID,它有诸如 sqft rons peices 之类的东西。我不需要全部填写,对吗?我每次都会出错 INSERT INTO 语句中的语法错误 [![在此处输入图片描述][1]][1]
您的 SQL 似乎拼错了。正确示例:
private void button1_Click(object sender, EventArgs e)
{
try
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dhelm.ALLMATINC.001\Desktop\Db11.accdb";
cmd.CommandText = @"insert into Table1 (Customer,Description,Color,Status) VALUES('" + tBox3.Text + "','" + tBox1.Text + "','" + tBox12.Text + "','" + cBox2.Text + "')";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show("Recrod Succefully Created");
con.Close();
}
catch(Exception ex)
{
MessageBox.Show("error " + ex);
}
}
您无需在 INSERT
语句中提供所有列的值。
如果字段设置为自动递增或允许 NULL
值,则可以省略这些字段。
在您的情况下,我认为您应该将主键 ID
设置为自动递增,其他字段允许 NULL
值。
您在 "Status" 字段后多了一个“,”。您不应将值直接附加到命令,而应使用参数化值。您也可以使用 "using" -statements 自动关闭和处理连接和命令。下面是可以帮助您解决问题的小代码:
using (var con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dhelm.ALLMATINC.001\Desktop\Db11.accdb"))
{
using (var cmd = new OleDbCommand(@"
INSERT INTO
Table1(Customer,Description,Color,Status)
VALUES(@Customer,@Description,@Color,@Status)
", con))
{
cmd.Parameters.Add(new OleDbParameter("@Customer", OleDbType.VarChar)).Value = tBox3.Text;
cmd.Parameters.Add(new OleDbParameter("@Description", OleDbType.VarChar)).Value = tBox1.Text;
cmd.Parameters.Add(new OleDbParameter("@Color", OleDbType.VarChar)).Value = tBox12.Text;
cmd.Parameters.Add(new OleDbParameter("@Status", OleDbType.VarChar)).Value = cBox2.Text;
con.Open();
cmd.ExecuteNonQuery();
System.Windows.Forms.MessageBox.Show("Recrod Succefully Created");
}
}