数据库last_insert_id第一条为什么是0(MYSQL+C#)

高分请讲解下,数据库last_insert_id第一条为什么是0(MYSQL+C#)?

先上图第一个表st_category_brand是记录刚插入的st_brand表中的记录 有耐心的看看吧

这张表st_brand

st_category

程序图 意思是在【中国】下面添加3个地址

程序代码
//方法
public void insert_brand(string arry, string cat_id)//向数据库中插入品牌数据
{
string conn = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconns"].ToString();
MySqlConnection mysql = new MySqlConnection(conn);//实例化链接
mysql.Open();//开启
string sql_brand = "insert into st_brand(brand_name) values ('" + arry + "')";//插入品牌
string sql_category_brand = "insert into st_category_brand(cat_id,brand_id) select cat_id,last_insert_id() from st_category where cat_id='" + cat_id + "' ";//关联品牌和类
MySqlCommand comm = new MySqlCommand(sql_brand, mysql);
MySqlCommand comms = new MySqlCommand(sql_category_brand, mysql);
comms.ExecuteNonQuery();//执行
comm.ExecuteNonQuery();//执行
mysql.Close();//关闭资源

}
----------------------------------------
调用方法
protected void Button5_Click(object sender, EventArgs e)
{
cns mysql = new cns();
string s = TextBox2.Text;
s = Regex.Replace(s, @" ", "\r\n");//遇到空格回车
TextBox2.Text = s.Trim(); //清除回车并清除首尾空字符
TextBox2.Text = Regex.Replace(this.TextBox2.Text, @"(?s)\n\s*\n", "\n");
//存入数组中
string[] arry1 = TextBox2.Text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < arry1.Count(); i++)
{
ListBox8.Items.Add(arry1[i].ToString());
if (Label3.Text == "1")
{
mysql.insert_brand(arry1[i].ToString(), ListBox9.SelectedValue);//调用发放传递参数
//执行品牌插入和品牌和类的关联方法
}
}
我已经解决了哪位回答直接送分喽! 改变一下sql执行顺序就解决了
最新回答
╭⌒浅痛

2025-06-21 05:01:40

路过 拿分 ,目测是传入 数据问题