如何在文本框中增加我的ID [英] how to increment my id in textbox
本文介绍了如何在文本框中增加我的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将我的文本框设置为自动增量
i有两个文本框
id
名字
和一个按钮
new
i在sql server 2008中有一个表
用户
当我点击新按钮
我的id文本框增加1
并显示在文本框中
在sql server中我已经将标识符增量设置为1
但是问题是如何在c sharp box框中执行此操作
i want to set my text box as auto increment
i have two text boxes
id
name
and one button
new
i have a table in sql server 2008
user
when i click on new button
my id textbox is incremented by 1
and display in textbox
in sql server i already set the identifier increment by one
but problem is how to do this in c sharp text box
推荐答案
不要在屏幕上设置它!您的用户不关心该号码。删除该文本框。
don't set it on screen! Your user does not care for the number. REMOVE that textbox.
在表单中加载事件
TextBox1.text = getID()。ToString();
private int getID()
{
尝试
{
int no = 0;
SqlCommand cmd = new SqlCommand(SELECT COUNT(*)as tablename,dbe.getConnection());
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
no = dr .GetInt32(no)+ 1;
}
dr.Close();
返回否;
}
catch(Exception ex)
{
MessageBox.Show(生成ID.\\\
时出错+ ex.Message,this.Text,MessageBoxButtons.OK, MessageBoxIcon.Error);
返回0;
}
}
In form Load Event
TextBox1.text= getID().ToString();
private int getID()
{
try
{
int no=0;
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) as tablename ", dbe.getConnection());
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
no = dr.GetInt32("no") + 1;
}
dr.Close();
return no;
}
catch (Exception ex)
{
MessageBox.Show("Error on generating ID.\n" + ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return 0;
}
}
这很简单。复制粘贴页面加载事件中的代码
It is very simple.copy paste the code in your page load event
string sqlText = "SELECT MAX(product_id) FROM Product ";
SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\QXScustomerportalDB.mdf;Integrated Security=True;User Instance=True");
SqlCommand command = new SqlCommand(sqlText, con3);
try
{
con3.Open();
string pcount = Convert.ToString( command.ExecuteScalar());
if (pcount.Length == 0)
{
TextBox1.Text = "1";
}
else
{
int pcount1 = Convert.ToInt32(pcount);
int pcountAdd = pcount1 + 1;
TextBox1.Text = pcountAdd.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con3.Close();
}
这篇关于如何在文本框中增加我的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文