简体   繁体   English

无法将数据插入SQL Server数据库

[英]Cannot insert data into SQL Server database

I have already read all the previous similar posts but I couldn't find a solution. 我已经阅读了所有以前的类似文章,但是找不到解决方案。 Could you please take a look at my code? 你能看看我的代码吗? I don't get any exception. 我没有例外。 I just don't see the new data in the database. 我只是在数据库中看不到新数据。

int admin = 23;

SqlConnection thisConnection = new SqlConnection(
     ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString);

SqlCommand nonqueryCommand = thisConnection.CreateCommand();

thisConnection.Open();

nonqueryCommand.CommandText = 
    "INSERT INTO Account (Username, Password, AdministratorId) VALUES (@username, @password, @admin)";

nonqueryCommand.Parameters.Add("@username", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@username"].Value = UsernameTextbox.Text.ToString();
nonqueryCommand.Parameters.Add("@password", SqlDbType.VarChar, 15);
nonqueryCommand.Parameters["@password"].Value = PasswordTextbox.Text.ToString();
nonqueryCommand.Parameters.Add("@admin", SqlDbType.Int);
nonqueryCommand.Parameters["@admin"].Value = admin;

nonquerycommand.ExecuteNonQuery();

thisConnection.Close();

You are indexing the connection strings collection with a connection string? 您正在使用连接字符串索引连接字符串集合吗?

int admin = 23;

  SqlConnection thisConnection = new SqlConnection(
"Data Source=...;Persist Security Info=True;User ID=myusername;Password=mypassword");
 SqlCommand nonqueryCommand = thisConnection.CreateCommand();
thisConnection.Open();
 nonqueryCommand.CommandText = "INSERT INTO Account (Username,Password,AdministratorId) VALUES (@username,@password,@admin)";

nonqueryCommand.Parameters.Add("@username", SqlDbType.VarChar, 20);
nonqueryCommand.Parameters["@username"].Value = UsernameTextbox.Text.ToString();
nonqueryCommand.Parameters.Add("@password", SqlDbType.VarChar, 15);
nonqueryCommand.Parameters["@password"].Value = PasswordTextbox.Text.ToString();
nonqueryCommand.Parameters.Add("@admin", SqlDbType.Int);
nonqueryCommand.Parameters["@admin"].Value = admin;

 nonquerycommand.ExecuteNonQuery();


 thisConnection.Close();

You need to fix up the connection string. 您需要修复连接字符串。

I'm not sure if this is a mistake or not, but the ConnectionStrings indexer in the ConfigurationManager is looking for the name of the connection string: 我不确定这是否是错误的,但是ConfigurationManagerConnectionStrings索引器正在寻找连接字符串的名称

<connectionStrings>
    <add name="SomeDB" connectionString="..." />
</connectionStrings>

ConfigurationManager.ConnectionStrings["SomeDB"].ConnectionString

I assume this would've caused an error in your code though, perhaps a null reference exception but I don't remember how that class works. 我以为这会在您的代码中引起错误,也许是空引用异常,但我不记得该类是如何工作的。

I think you are pulling the connection string incorrectly. 我认为您不正确地拉了连接字符串。 The ConfigurationManager.Connectionstrings is keyed on the name of the connection string in the app/web.config file. ConfigurationManager.Connectionstrings键入app / web.config文件中的连接字符串名称。 Try using the name in the .config file or just hard-coding the connection string into the SqlConnection objects constructor. 尝试使用.config文件中的名称,或仅将连接字符串硬编码到SqlConnection对象构造函数中。

SqlConnection thisConnection = new SqlConnection(
    ConfigurationManager.ConnectionStrings["NameOfConnectionString"].ConnectionString); 

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

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