每次单击“保存”按钮时,如何自动增加textBox1(ID)中的值?我的问题是textBox1中的值仅在每次运行winform时递增。
例如,我运行winform,ID的最后一个值为2,textbox1将生成数字3,单击保存按钮后,它不会生成下一个数字,而只会在关闭时增加表格并再次重新运行。 plsss帮助..
表单加载
private void Form1_Load(object sender, EventArgs e)
{
int a;
String path1 = "Data Source=LOCALHOST; Initial Catalog= ss; username=root; password=''";
MySqlConnection con = new MySqlConnection(path1);
con.Open();
string query = "Select Max(ID) from inc";
MySqlCommand cmd = new MySqlCommand(query, con);
MySqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string val = dr[0].ToString();
if (val == "")
{
textBox1.Text = "1";
}
else
{
a = Convert.ToInt32(dr[0].ToString());
a = a + 1;
textBox1.Text = a.ToString();
}
}
}
在保存按钮
中 private void button1_Click(object sender, EventArgs e)
{
String path = "Data Source=LOCALHOST; Initial Catalog= ss; username=root; password=''";
MySqlConnection sqlconn = new MySqlConnection(path); //communicator //constructors
MySqlCommand sqlcomm = new MySqlCommand();
MySqlDataReader sqldr;
sqlconn.Open();
sqlcomm.Connection = sqlconn;
sqlcomm.CommandType = CommandType.Text;
sqlcomm.CommandText = "Select * from inc where ID=" + textBox1.Text + "";
sqldr = sqlcomm.ExecuteReader();
sqldr.Read();
if (sqldr.HasRows)
{
textBox3.Text = sqldr[0].ToString();
}
sqlconn.Close();
if (textBox1.Text == textBox3.Text)
{
MessageBox.Show("ID already exists!");
}
else
{
String path1 = "Data Source=LOCALHOST; Initial Catalog= ss; username=root; password=''";
MySqlConnection sqlconnn = new MySqlConnection(path1); //communicator //constructors
MySqlCommand sqlcommm = new MySqlCommand();
sqlconnn.Open();
sqlcommm.Connection = sqlconnn;
sqlcommm.CommandType = CommandType.Text;
sqlcommm.CommandText = "INSERT INTO inc (ID,Lastname) VALUES ("+ textBox1.Text +", '" + textBox2.Text + "')";
sqlcommm.ExecuteNonQuery();
sqlconnn.Close();
MessageBox.Show("RECORD SAVED!");
}
}
答案 0 :(得分:0)
只需要保存记录
后更新textBox1.Text...
sqlcommm.ExecuteNonQuery();
sqlconnn.Close();
MessageBox.Show("RECORD SAVED!");
//Update textBox1.Text
....
您必须为下次保存生成下一个值!
答案 1 :(得分:0)
private void Form1_Load(object sender, EventArgs e)
{
int a;
String path1 = "Data Source=LOCALHOST; Initial Catalog= ss; username=root; password=''";
MySqlConnection con = new MySqlConnection(path1);
con.Open();
string query = "Select Max(ID) from inc";
MySqlCommand cmd = new MySqlCommand(query, con);
MySqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string val = dr[0].ToString();
setText(val );
}
}
private void button1_Click(object sender, EventArgs e)
{
String path = "Data Source=LOCALHOST; Initial Catalog= ss; username=root; password=''";
MySqlConnection sqlconn = new MySqlConnection(path); //communicator //constructors
MySqlCommand sqlcomm = new MySqlCommand();
MySqlDataReader sqldr;
sqlconn.Open();
sqlcomm.Connection = sqlconn;
sqlcomm.CommandType = CommandType.Text;
sqlcomm.CommandText = "Select * from inc where ID=" + textBox1.Text + "";
sqldr = sqlcomm.ExecuteReader();
sqldr.Read();
if (sqldr.HasRows)
{
textBox3.Text = sqldr[0].ToString();
}
sqlconn.Close();
if (textBox1.Text == textBox3.Text)
{
MessageBox.Show("ID already exists!");
}
else
{
String path1 = "Data Source=LOCALHOST; Initial Catalog= ss; username=root; password=''";
MySqlConnection sqlconnn = new MySqlConnection(path1); //communicator //constructors
MySqlCommand sqlcommm = new MySqlCommand();
sqlconnn.Open();
sqlcommm.Connection = sqlconnn;
sqlcommm.CommandType = CommandType.Text;
sqlcommm.CommandText = "INSERT INTO inc (ID,Lastname) VALUES ("+ textBox1.Text +", '" + textBox2.Text + "')";
sqlcommm.ExecuteNonQuery();
sqlconnn.Close();
MessageBox.Show("RECORD SAVED!");
setText( textBox1.Text);
}
}
private void setText(string newValue)
{
if (newValue == "")
{
textBox1.Text = "1";
}
else
{
a = Convert.ToInt32(newValue);
a = a + 1;
textBox1.Text = a.ToString();
}
}
您需要在每次成功保存时更新ID。
答案 2 :(得分:-1)
试试这个
connect <- function(userName,pwd){
isConnected <<- 0;
qry = paste0("SELECT password from USER where pseudo = \'",userName,"\'")
res= dbGetQuery(db,qry )
res = paste0(res)
if(res==pwd)
{
isConnected <<- 1;
print("CONNECTED")
}
else{
print("unable to connect to the database")
}
function(input, output, session) {
isConnectedVar <- reactive({
isConnected+1
})
#Authentification Panel dynamic UI
observe({
if(isConnected== 0){
reactValues$selector <<- updateSelectInput(session,"selectModelInput", label="Model selection", choices = as.character(df[[paste0(option,isConnectedVar())]]))
}
else{
reactValues$selector <<- updateSelectInput(session,"selectModelInput", label="Model selection", choices = as.character(df[[paste0(option,isConnectedVar())]]))
}
})
observeEvent(input$connectButton, {
userName= paste0(input$username)
userPwd = paste0(input$password)
connect(user = userName,pwd = userPwd)
})
然后
if (!Page.IsPostBack) {
ViewState["hereYourThingToIncrement"] = "1";
yourtxt.Text = ViewState["hereYourThingToIncrement"].ToString();
}