简体   繁体   English

从C#将整数值插入SQL Server数据库

[英]Insert integer value into SQL Server database from C#

I am trying to insert an integer value into a SQL Server database as below when I run the program there are no any errors, but the table doesn't get updated with values. 当我运行程序时,我尝试将一个整数值插入SQL Server数据库,如下所示,没有任何错误,但表未更新为值。 I have searched on the internet and I am doing the same can anyone help to find what I am doing wrong. 我已经在互联网上进行搜索,并且我正在做同样的事情,任何人都可以帮助找到我做错了什么。

Note: I already defined "connectionString" as a string on the form class 注意:我已经在表单类上将“ connectionString”定义为字符串

private void btnUpdate_Click(object sender, EventArgs e)
{
        int totalincome=600;
        int totaldeductions = 10;

        connectionString = ConfigurationManager.ConnectionStrings["BudgetApp.Properties.Settings.MainDataBaseConnectionString"].ConnectionString;

        con = new SqlConnection(connectionString);

        con.Open();

        cmd = new SqlCommand("INSERT INTO Totals(TotalIncome, TotalDeductions) VALUES (@TotalIncome, @TotalDeductions)", con);
        cmd.Parameters.AddWithValue("@TotalIncome", totalincome);
        cmd.Parameters.AddWithValue("@TotalDeductions", totaldeductions);

        cmd.ExecuteNonQuery();

        MessageBox.Show("Done !!");
}

The whole AttachDbFileName= approach is flawed - at best! 整个AttachDbFileName =方法有缺陷-充其量! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\\bin\\debug - where you app runs) and most likely , your INSERT works just fine - but you're just looking at the wrong .mdf file in the end! 在Visual Studio中运行应用程序时,它将在.mdf文件周围复制(从App_Data目录复制到输出目录-通常是.\\bin\\debug应用程序运行所在的位置),并且很可能 INSERT可以正常工作-但最后您只是看错了.mdf文件

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there. 如果您要坚持这种方法,请尝试在myConnection.Close()调用上放置一个断点,然后使用SQL Server Mgmt Studio Express检查.mdf文件-我几乎可以确定您的数据在那里。

The real solution in my opinion would be to 我认为真正的解决方案

  1. install SQL Server Express (and you've already done that anyway) 安装SQL Server Express(并且您已经完成了此操作)

  2. install SQL Server Management Studio Express 安装SQL Server Management Studio Express

  3. create your database in SSMS Express , give it a logical name (eg MainDataBase ) SSMS Express中创建数据库,给它一个逻辑名(例如MainDataBase

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. 使用其逻辑数据库名称 (在服务器上创建时提供)连接到该数据库 -并且不要弄乱物理数据库文件和用户实例。 In that case, your connection string would be something like: 在这种情况下,您的连接字符串将类似于:

     Data Source=.\\\\SQLEXPRESS;Database=MainDataBase;Integrated Security=True 

    and everything else is exactly the same as before... 和其他一切是完全一样的前...

Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info. 另请参阅亚伦·贝特朗(Aaron Bertrand)出色的博客文章不良习惯:使用AttachDbFileName获取更多背景信息。

Code Seems correct,Perhaps you are checking the wrong DB?. 代码似乎正确,也许您正在检查错误的数据库? I would add a Try/catch for exceptions. 我会为异常添加一个try / catch。 And remember to close connection after executing query. 并记住在执行查询后关闭连接。 Regards 问候

check your database column datatype,use try catch. 检查您的数据库列数据类型,使用try catch。

and try to replace cmd.Parameters.AddWithValue("@TotalIncome", totalincome); 并尝试替换cmd.Parameters.AddWithValue("@TotalIncome", totalincome); to cmd.Parameters.Add("@Number", SqlDbType.Int).Value = totalincome; cmd.Parameters.Add("@Number", SqlDbType.Int).Value = totalincome;

try
{
       int totalincome=600;
        int totaldeductions = 10;
        connectionString = ConfigurationManager.ConnectionStrings["BudgetApp.Properties.Settings.MainDataBaseConnectionString"].ConnectionString;
        con = new SqlConnection(connectionString);
        con.Open();
        cmd = new SqlCommand(@"INSERT INTO Totals(TotalIncome, TotalDeductions) VALUES (@TotalIncome, @TotalDeductions)", con);

        cmd.Parameters.Add("@Number", SqlDbType.Int).Value = totalincome;
        cmd.Parameters.Add("@Number", SqlDbType.Int).Value = totaldeductions;
        //cmd.Parameters.AddWithValue("@TotalIncome", totalincome);
        //cmd.Parameters.AddWithValue("@TotalDeductions", totaldeductions);

        cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
  MessageBox.Show(ex.ToString());
}

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

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