[英]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更新的值。
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.