帮助任何人,
我是C#和使用数据库的新手。我正在尝试使用Youtube的一些代码将信息插入数据库。我能够从数据库中成功选择数据,但是由于任何原因,当我尝试执行下面的插入查询(在下面加粗体和下划线)时,我的try catch块都会引发与语法错误相关的异常并引用我的插入语句:
我是C#和使用数据库的新手。我正在尝试使用Youtube的一些代码将信息插入数据库。我能够从数据库中成功选择数据,但是由于任何原因,当我尝试执行下面的插入查询(在下面加粗体和下划线)时,我的try catch块都会引发与语法错误相关的异常并引用我的插入语句:
C#:
namespace Movies
{
public partial class Form2 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form2()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Graham\Desktop\School.accdb;
Persist Security Info=False;";
}
private void btn_save_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
//command.CommandText = "insert into students(Names, Age, Gpa) values('" + txt_name.Text +"','" + txt_age.Text + "','" + txt_gpa.Text + "')";
[U][B]command.CommandText = "INSERT INTO students (Names, Age, Gpa) VALUES ('Abeni', '20', '3.2')";[/B][/U]
int numRows = command.ExecuteNonQuery();
MessageBox.Show("Data Saved");
}
catch(Exception excpt)
{
MessageBox.Show("Error" + excpt);
}
}
}
}
由主持人最后编辑: