在C#winform中怎么让添加一些参数后,这行数据的ID自动加一

图片说明

 private void btnAdd_Click(object sender, EventArgs e)
        {
            string dbPath = @"D:\历年高考分数.db";
            if (btnName == "添加")
            {
                if (File.Exists(dbPath))
                {

                    string sq = @"Data Source=" + dbPath;
                    m = new SQLiteConnection(sq);
                    m.Open();
                    string sql = "select * from sqlite_master where type='table' and name ='特性'";
                    SQLiteCommand command = new SQLiteCommand(sql, m);//
                    //m.Close();
                    //AddcailiaoPara1(dbPath, "特性");
                    if (command.ExecuteScalar() == null)
                    {
                        m.Close();
                        NewTable1(dbPath, "特性");
                        AddcailiaoPara1(dbPath, "特性");
                    }
                    else
                    {
                        m.Close();
                        AddcailiaoPara1(dbPath, "特性");
                    }

                }
                else
                {
                    NewDbFile(dbPath);
                    NewTable1(dbPath, "特性");
                    AddcailiaoPara1(dbPath, "特性");
                }
                Guanlicailiaoquxian.form1.LoadcailiaoPara();
                DialogResult result = MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        public void AddcailiaoPara1(string dbPath, string tableName)
        {
            string yinsu = "";
            if (radioButton2.Checked == true) { yinsu = "紫外线"; }
            else if (radioButton1.Checked == true) { yinsu = "温度、振动"; }
            string id = ?
            //string tabText = txtName.Text + yinsu;
            //string str = "'" + txtName.Text + "'," + "'" + txtyunxinglicheng.Text + "'," + "'" + txtyingdu.Text + "'," + "'" + txtlashenqiangdu + "'," + "'" + txtladuanshenchanglv + "'," + "'" + yinsu + "'," + "'" + id + "'";
            string str = "'" + txtName.Text + "'," + "'" + txtyunxinglicheng.Text + "'," + "'" + txtyingdu.Text + "'," + "'" + txtlashenqiangdu.Text + "'," + "'" + txtladuanshenchanglv.Text + "'," + "'" + yinsu + "'," + "'" + id + "'";
            string sq = @"Data Source=" + dbPath;
            m = new SQLiteConnection(sq);
            m.Open();
            string sql = "insert into " + tableName + " (传感器型号,运行里程,硬度,拉伸强度,拉断伸长率,因素,材料特性ID) values (" + str + ")";
            SQLiteCommand command = new SQLiteCommand(sql, m);
            command.ExecuteNonQuery();
            m.Close();
        }

怎么让添加一些参数后,这行数据的ID自动加一,以便于以后根据id删除数据
string id那应该怎么写

2个回答

weixin_44138934
wxy新人小白 按添加后,若mid,id都是string型,command.ExecuteNonQuery();处显示datatype mismatch
11 个月之前 回复
weixin_44138934
wxy新人小白 string zid = "select max(材料特性ID) from 特性"; int mid = 1; int id = Convert.ToInt32(zid) + mid; string str = "'" + txtName.Text + "'," + "'" + txtyunxinglicheng.Text + "'," + "'" + txtyingdu.Text + "'," + "'" + txtlashenqiangdu.Text + "'," + "'" + txtladuanshenchanglv.Text + "'," + "'" + yinsu + "'," + "'" + id + "'";这样写,显示 输入字符串的格式不正确。
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复wxy新人小白: 那你试试看 select max(材料特性ID) from table
11 个月之前 回复
weixin_44138934
wxy新人小白 若把材料特性ID设为主键, command.ExecuteNonQuery();处显示数据类型不匹配
11 个月之前 回复
weixin_44138934
wxy新人小白 我那列材料特性ID(id填充)它不是主键
11 个月之前 回复

添加前查询最后一条数据的id,然后用上一条的id+1再做添加,简单实用

weixin_42322532
开局一个bug string s = "123"; int k = int.Parse(s) + 1;
11 个月之前 回复
weixin_42322532
开局一个bug 回复wxy新人小白: string 转 int
11 个月之前 回复
weixin_44138934
wxy新人小白 我就是想这样做,不是格式不正确吗
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐