如何将我的SQL数据库中的int值显示到winform中的文本框中 [英] How to display an int value frm my SQL database to a textbox in my winform

查看:260
本文介绍了如何将我的SQL数据库中的int值显示到winform中的文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示一个恰好是数据库id的int值。我希望在单击保存按钮之后在其中一个文本框中显示int值,该保存按钮将插入到数据库表中,之后它应该在其中一个文本框中显示数据库中自动增量的id



我尝试过:



con = new SqlConnection(cs.DBcon);



con.Open();



DateTime date = DateTime.Parse(dateTimePicker1.Text);

string cb =插入Sales(Date,CustomerName,MealType,ChargeType,Details,Amount,PostedBy)VALUES(@ d1,@ d2,@ d3,@ d4,@ d5,@ d6, @ D7);





cmd =新的SqlCommand(cb);



cmd。 Connection = con;

cmd.Parameters.AddWithValue(@ d1,date);

cmd.Parameters.AddWithValue(@ d2,textBox1.Text);

cmd.Parameters.AddWithValue(@ d3,comboBox1.Text);

cmd.Parameters.AddWithValue(@ d4,comboBox2.Text);

cmd.Parameters.AddWithValue(@ d5,textBox3.Text);

cmd.Parameters.AddWithValue(@ d6,textBox2.Text);

cmd.Parameters.AddWithValue(@ d7,lblUser.Text);

cmd.ExecuteReader();

con.Close();

MessageBox.Show(成功售票);



MealTicket loadid = new MealTicket();

DataSet ds = new DataSet();

string id;

id = textBox1.Text;

ds = loadid.Getid(id);

if(ds.Tables [0] .Rows.Count> 0)

{

textBox4.Text = ds.Tables [0 ] .Rows [0] [ TRANSID] .ToString();



}



}

I tried to display an int value which happens to be the id the database. i want the int value to show after in one of the textbox after clicking the save button which would have inserted into the database table and after that it should display the id which is auto increment in the database in one of the textbox

What I have tried:

con = new SqlConnection(cs.DBcon);

con.Open();

DateTime date = DateTime.Parse(dateTimePicker1.Text);
string cb = "insert into Sales (Date,CustomerName,MealType,ChargeType,Details,Amount,PostedBy)VALUES(@d1,@d2,@d3,@d4,@d5,@d6,@d7)";


cmd = new SqlCommand(cb);

cmd.Connection = con;
cmd.Parameters.AddWithValue("@d1", date);
cmd.Parameters.AddWithValue("@d2", textBox1.Text);
cmd.Parameters.AddWithValue("@d3", comboBox1.Text);
cmd.Parameters.AddWithValue("@d4", comboBox2.Text);
cmd.Parameters.AddWithValue("@d5", textBox3.Text);
cmd.Parameters.AddWithValue("@d6", textBox2.Text);
cmd.Parameters.AddWithValue("@d7", lblUser.Text);
cmd.ExecuteReader();
con.Close();
MessageBox.Show("Meal Ticket Successfully Sold");

MealTicket loadid = new MealTicket();
DataSet ds = new DataSet ();
string id;
id = textBox1.Text;
ds = loadid.Getid(id);
if (ds.Tables[0].Rows.Count > 0)
{
textBox4.Text = ds.Tables[0].Rows[0]["TransID"].ToString();

}

}

推荐答案

我解释这个问题的方式是你希望能够在插入一些新数据后显示由Identity列生成的新ID。



此MSDN文章,检索身份或自动编号值 [ ^ ],提供一些建议。



例如,您可以将您的sql更改为
The way I am interpreting this question is that you want to be able to display the new ID generated by the Identity column after you insert some new data.

This MSDN article, Retrieving Identity or Autonumber Values[^], offers some suggestions.

For example you could change your sql to be
string cb = "insert into Sales (Date,CustomerName,MealType,ChargeType,Details,Amount,PostedBy) OUTPUT INSERTED.ID VALUES(@d1,@d2,@d3,@d4,@d5,@d6,@d7)";



无论如何,


You have a problem anyway with

cmd.ExecuteReader();

您没有尝试读取您要插入的数据。所以至少你需要

You are not trying to read data you are trying to insert it. So at the very least you would need

cmd.ExecuteNonQuery();



但是,如果你按照我上面的建议更改你的查询那么你可以这样做


However, if you change your query as I suggested above then you could do this

textBox4.Text = cmd.ExecuteScalar().ToString();


这篇关于如何将我的SQL数据库中的int值显示到winform中的文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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