C#INSERT INTO语句C中的语法错误#

C#INSERT INTO语句C中的语法错误#,c#,C#,每次我运行这个,它总是显示错误。我不知道如何修理它。 代码应该将我放在文本框中的数据记录到ms access数据库中。plz helpp大概,您已经在某个地方通过如下操作初始化了cn if (txtUsername.Text != "") { string q = "insert into info(Username) values ('" + txtUsername.Text.ToString() + "')"; dosometh

每次我运行这个,它总是显示错误。我不知道如何修理它。
代码应该将我放在文本框中的数据记录到ms access数据库中。plz helpp

大概,您已经在某个地方通过如下操作初始化了cn

if (txtUsername.Text != "")
        {
            string q = "insert into info(Username) values ('" + txtUsername.Text.ToString() + "')";
            dosomething(q);
            txtUsername.Text = "";
        }
        else
        {
            MessageBox.Show("Please Complete the neccessary information");
        }
        if (txtPassword.Text != "")
        {
            string a = "insert into info(Password) values ('" + txtPassword.Text.ToString() + "')";
            dosomething(a);
            txtUsername.Text = "";

        }
        else
        {
            MessageBox.Show("Please Complete the neccessary information");
        }

private void dosomething(String q)
{
    try
    {
        cn.Open();
        cmd.CommandText = q;
        cmd.ExecuteNonQuery();
        cn.Close();

    }
    catch (Exception e)
    {
        cn.Close();
        MessageBox.Show(e.Message.ToString());
    }
}
您需要将数据库的连接字符串传递给构造函数:

cn = new SqlConnection();
或者稍后在连接之前设置:

cn = new SqlConnection("your connection string here");

您在哪里定义了cn?您是如何将connectionstring传递给它的?请在postcn=connection对象中包含连接字符串的代码,您将把connectionstring传递给。。。读起来,我知道这是一个初学者的代码,但有这么多的东西错了。。。
cn.ConnectionString = "your connection string here";