不能将数据插入到数据库 [英] cannot insert data into database

查看:150
本文介绍了不能将数据插入到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想什么我的标题says.I已经阅读所有的previous类似的职位,但我无法找到一个解决方案.Could请你拿手表到我的code?
编辑:我没有得到任何exception.I只是没有看到在数据库中的新数据
EDIT2:前4个答案,不解决我的问题,因为我编辑了code和添加的ExecuteNonQuery命令。

  INT管理员= 23;
SqlConnection的thisConnection =新的SqlConnection(
    ConfigurationManager.ConnectionStrings [
    数据源= ...;坚持安全信息= TRUE;用户ID =名为myusername;密码=输入mypassword]
    .ConnectionString);
的SqlCommand nonqueryCommand = thisConnection.CreateCommand();
thisConnection.Open();
nonqueryCommand.CommandText =INSERT INTO账户(用户名,密码,AdministratorId)VALUES(@用户名,密码@,@管理员);nonqueryCommand.Parameters.Add(@用户名,SqlDbType.VarChar,20);
。nonqueryCommand.Parameters [@用户名]值= UsernameTextbox.Text.ToString();
nonqueryCommand.Parameters.Add(@密码,SqlDbType.VarChar,15);
nonqueryCommand.Parameters [@密码]值= PasswordTextbox.Text.ToString()。
nonqueryCommand.Parameters.Add(@管理,SqlDbType.Int);
。nonqueryCommand.Parameters [@管理员]值=管理员;编辑:nonquerycommand.ExecuteNonQuery();
thisConnection.Close();


解决方案

您似乎没有被实际执行查询。执行它在您关闭连接之前。

  nonquerycommand.ExecuteNonQuery();

I just want what my title says.I have already read all the previous similar posts but i couldn't find a solution .Could you please take a watch into my code? EDIT:I don't get any exception.I just don't see the new data in the database EDIT2:The first 4 answers don't solve my problem because i edited the code and added the executenonquery command.

int admin = 23;
SqlConnection thisConnection = new SqlConnection(
    ConfigurationManager.ConnectionStrings[
    "Data Source=...;Persist Security Info=True;User ID=myusername;Password=mypassword"]
    .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;

EDIT:nonquerycommand.ExecuteNonQuery();


thisConnection.Close();

解决方案

You don't seem to be actually executing your query. Execute it before you close your connection.

nonquerycommand.ExecuteNonQuery();

这篇关于不能将数据插入到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆