简体   繁体   English

无法使用C#将数据插入SQL数据库

[英]Unable to insert data into SQL Database using C#

I'm writing a method to insert a Student into a local SQL database that contains a table with information about Students: 我正在编写一个方法将Student插入到本地SQL数据库中,该数据库包含一个包含有关Students的信息的表:

public void AddStudent(string name, string teachName, string pass)
{
    string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\Logo.sdf";
    SqlCeConnection connection = new SqlCeConnection("Data Source=" + dbfile + "; Password = 'dbpass2011!'");
    connection.Open();

    SqlCeTransaction transaction = connection.BeginTransaction();
    SqlCeCommand command = connection.CreateCommand();
    command.Transaction = transaction;
    command.CommandText = "INSERT INTO Students VALUES ('@name', '@id', '@pass', '@tname')";
    command.Parameters.Add("@name", name);
    command.Parameters.Add("@id", this.ID);
    command.Parameters.Add("@pass", MD5Encrypt.MD5(pass));
    command.Parameters.Add("@tname", teachName);


    command.Prepare();
    command.ExecuteNonQuery();
    transaction.Commit();
    connection.Dispose();
    connection.Close();
}

Whenever I use this, it never inserts the data to the table when I look at the contents of the Students table in the database. 每当我使用它时,当我查看数据库中Students表的内容时,它永远不会将数据插入到表中。 Originally I had this return an int so I could see how many rows it affected, which it always returned 1, so I know it's working. 最初我有这个返回一个int所以我可以看到它影响了多少行,它总是返回1,所以我知道它正在工作。

I've looked for answers to this, and the answer to similar questions was that the person asking was looking at the wrong .sdf file. 我已经找到了答案,类似问题的答案就是提问者正在查看错误的.sdf文件。 I've made sure that I'm looking at the right file. 我已经确定我正在查看正确的文件。

Any feedback would be much appreciated! 任何反馈将不胜感激!

command.CommandText = "INSERT INTO Students VALUES ('@name', '@id', '@pass', '@tname')";

You should remove the extra single quotes - this should be: 你应该删除额外的单引号 - 这应该是:

command.CommandText = "INSERT INTO Students VALUES (@name, @id, @pass, @tname)";

Also I am not sure why you open a transaction for a single insert - that is also not needed. 此外,我不确定为什么要为单个插入打开事务 - 这也是不需要的。

There is most likely an exception being raised in your code; 您的代码中很可能会引发异常; you need to add a try/catch handler and/or debug the application to figure out exactly what is happening. 你需要添加一个try / catch处理程序和/或调试应用程序以确切地弄清楚发生了什么。

However, there are at least two issues with your code: 但是,您的代码至少存在两个问题:

  • The prepare statement requires the data types of the parameters. prepare语句需要参数的数据类型。 From the MSDN documentation : MSDN文档

Before you call Prepare, specify the data type of each parameter in the statement to be prepared. 在调用Prepare之前,请在要准备的语句中指定每个参数的数据类型。 For each parameter that has a variable-length data type, you must set the Size property to the maximum size needed. 对于具有可变长度数据类型的每个参数,必须将Size属性设置为所需的最大大小。 Prepare returns an error if these conditions are not met. 如果不满足这些条件,准备将返回错误。

  • You need to close the connection before disposing it (this won't affect the insert, however). 您需要在处理之前关闭连接(但这不会影响插入)。

You don't need to put single quote to parametrized query, in case of parametrized query the whole data will be parsed as required, 您不需要将单引号放到参数化查询中,在参数化查询的情况下,将根据需要解析整个数据,

command.CommandText = "INSERT INTO Students VALUES (@name, @id, @pass, @tname)";

Also, its better to set parameter type, size and value explicitly as below: 此外,最好明确设置参数类型,大小和值,如下所示:

SqlCeParameter param = new SqlCeParameter("@name", SqlDbType.NVarChar, 100);
param.Value  = name; // name is a variable that contain the data of name field
//param.Value  = 'Jhon Smith';  //Directly value also can be used

Hope this would be helpful, thanks for your time. 希望这会有所帮助,谢谢你的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM