简体   繁体   English

SQL无法在C#上运行,但可以在SQL Server Management Studio中运行

[英]SQL not working on c# but working in SQL Server Management Studio

Update should be updating the data in the confirm table with the given parameters. 更新应使用给定参数更新确认表中的数据。 However no input gets updated/inputted despite there being no errors. 但是,即使没有错误,也不会更新/输入任何输入。 When the exact same query is inputted into the SQL Server Management Studio there is no errors and the rows are updated. 当将完全相同的查询输入到SQL Server Management Studio中时,没有错误,并且行被更新。

Why is the table not being updated? 为什么不更新表?

There are 3 columns in the table - orderid (which is passed from another table) and then staffid and confirmed which should both be NULL - and are - until the rows are updated. 该表中有3列-orderid(从另一个表传递来的),然后是staffid并确认,它们都应该为NULL-并且是-直到更新行。 orderid = int not null, staffid = int, confirmed = string. orderid = int不为null,staffid = int,已确认=字符串。 confirm database 确认数据库

The view is a left outer join, meaning that it shows the values that need to be update by the by. 该视图是左外部联接,这意味着它显示了需要由by更新的值。

[sql ][2] [sql ] [2]

database diagram 数据库图

Form 形成

How can this be fixed, its been like this for two days. 这怎么解决,这样已经两天了。

  using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;

    namespace ComicBookShop
    {
        public partial class orders_confirm : Form
        {

            public orders_confirm()
            {
                InitializeComponent();
            }
            //database details
            string connString = "Data Source = BLAH BLAH BLAH";


            private void btnBack_Click(object sender, EventArgs e)
            {
                this.Hide();
                ManagmentMain fm = new ManagmentMain();
                fm.Show();
            }

            private void orders_confirm_Load(object sender, EventArgs e)
            {

                SqlConnection con = new SqlConnection(connString);
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM staff_view", con))
                    {
                        cmd.CommandType = CommandType.Text;
                        using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                        {
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);

                                //Set AutoGenerateColumns False
                                dataGridView5.AutoGenerateColumns = true;


                                dataGridView5.DataSource = dt;

                            }
                        }
                    }


                    con.Close();
                }

            }
            private void btnUpdate_Click(object sender, EventArgs e)
            {
                if (txtConfirmed.Text == "" || txtorder.Text == "" || txtstaff.Text == "")
                {
                    MessageBox.Show("Please fill textboxes");
                    return;
                }
                //database details
                string connString = "Data Source = aak; Initial Catalog = aa; User ID = aa; Password = aa";
                SqlConnection con = new SqlConnection(connString);
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    using (SqlCommand command = con.CreateCommand())
                    {
                        try
                        {

                            con.Open();
                            command.CommandText = "Update dbo.confirm set staffid=@staffid, confirmed=@confirmed where orderid =@orderid";

                            command.Parameters.AddWithValue("@orderid", txtorder.Text);
                            command.Parameters.AddWithValue("@staffid", txtstaff.Text);
                            command.Parameters.AddWithValue("@confirmed", txtConfirmed.Text);

                            command.ExecuteNonQuery();

                            con.Close();
                            MessageBox.Show("Updated");

                        }

                        catch (SqlException ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }


            }
        }
    }

this is the part of the code where the data should be inserted 这是应在其中插入数据的代码部分

 private void btnUpdate_Click(object sender, EventArgs e)
            {
                if (txtConfirmed.Text == "" || txtorder.Text == "" || txtstaff.Text == "")
                {
                    MessageBox.Show("Please fill textboxes");
                    return;
                }
                //database details
                string connString = "Data Source = aak; Initial Catalog = aa; User ID = aa; Password = aa";
                SqlConnection con = new SqlConnection(connString);
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    using (SqlCommand command = con.CreateCommand())
                    {
                        try
                        {

                            con.Open();
                            command.CommandText = "Update dbo.confirm set staffid=@staffid, confirmed=@confirmed where orderid =@orderid";

                            command.Parameters.AddWithValue("@orderid", txtorder.Text);
                            command.Parameters.AddWithValue("@staffid", txtstaff.Text);
                            command.Parameters.AddWithValue("@confirmed", txtConfirmed.Text);

                            command.ExecuteNonQuery();

                            con.Close();
                            MessageBox.Show("Updated");

                        }

                        catch (SqlException ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }

Try setting parameters with their corresponding data types: 尝试使用相应的数据类型设置参数:

 command.Parameters.Add("orderid", SqlDbType.Int);
 command.Parameters["orderid"].Value = int.Parse(txtorder.Text);

Do the same for staffid . staffid

I think the issue is you are passing string where int is expected. 我认为问题是您正在传递期望int string

暂无
暂无

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

相关问题 C# - XLSX 到 SQL Server - OpenRecordSet 不适用于 C#,但适用于 MS SQL Server Management Studio - C# - XLSX to SQL Server - OpenRecordSet not working in C# but works in MS SQL Server Management Studio SQL 请求在 SQL Server Management Studio 中工作正常,但使用 sqlCommand C# 发送时返回错误 - SQL request working fine in SQL Server Management Studio but returning an error when send with sqlCommand C# 在未安装SQL Server Management Studio的情况下运行C#应用程序 - Run C# application without SQL Server Management Studio installed 更新无法在SQL Server CE C#中工作 - Update not working in SQL Server CE C# 在C#和SQL Server中使用MultiLevel XML - Working with MultiLevel XML in C# and SQL Server 在C#中使用SQL Server元数据 - Working with sql server metadata in c# 从C#文件夹中使用特定的服务器名称打开.sql文件SQL Server Management Studio - Open .sql file SQL Server Management Studio from a folder throught C# with a specfic server name SQL Server 2008 datetime不会通过c#插入,但可以在SQL Server Management Studio中使用吗? - SQL Server 2008 datetime will not insert via c# but works within SQL Server Management Studio? 从C#运行时,SQL查询超时,在SQL Server Management Studio中快速 - SQL query times out when run from C#, fast in SQL Server Management Studio C#和SQL Server Management Studio中SQL查询的执行时间完全不同 - Very different execution times of SQL query in C# and SQL Server Management Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM