C# 在不知道主键的情况下插入多个表
嘿,伙计们,这里有点复杂,我有一个创建帐户页面,它只是将数据插入mysql数据库:C# 在不知道主键的情况下插入多个表,c#,asp.net,mysql,sql,html,C#,Asp.net,Mysql,Sql,Html,嘿,伙计们,这里有点复杂,我有一个创建帐户页面,它只是将数据插入mysql数据库: protected void Button1_Click(object sender, EventArgs e) { OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=
protected void Button1_Click(object sender, EventArgs e)
{
OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;");
cn.Open();
OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme, username, password) VALUES ('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "', '" + TextBox5.Text + "', '" + TextBox6.Text + "', '" + TextBox7.Text + "', '" + TextBox8.Text + "')", cn);
cmd.ExecuteNonQuery();
{
//e.Authenticated = true;
Response.Redirect("Login.aspx");
// Event useradded is true forward to login
}
}
}
但我在创建帐户页面上遇到了一个问题,我添加了一个fileupload控件,我想上传一个图像并将imageurl保存在pictures表中:
string filenameDB = Path.GetFileName(FileUploadControl.FileName);
string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(fileuploadpath);
string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") + filenameDB;
StatusLabel.Text = "Upload status: File uploaded!";
OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures VALUES picturepath ='" + fileuploadpaths + "' WHERE UserId = '" + theuserid + "'", cn);
cmd.ExecuteNonQuery();
第一个问题是sql语法,我需要将fileupload与我的buttonclick组合起来,这样它将被插入到两个表User和Pictures中,但接下来的问题是,如果帐户尚未创建,我究竟如何获取用户ID?啊哈哈
表结构:
因此,总而言之,我需要将用户详细信息插入用户表并上传到项目文件中,并将imageUrl插入图片表中(存储方式为So~/userdata/2/uploadedimages/bla.jpg)正如您所见,pictures表与user表是1-1关系,因此它依赖于创建帐户的userid,因此没有userid,因此不确定是否有方法交错代码,因此首先插入用户详细信息,然后使用会话检索该userid,然后将imageurl插入pictures表
或者可能有一些时髦的函数,一些聪明的人已经发现了这个问题,或者可能它只是一个简单的sql语法解构器
p.S我知道SQL注入的风险,请不要就此发表文章。谢谢大家强>
编辑:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;");
cn.Open();
OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme, username, password) VALUES ('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "', '" + TextBox5.Text + "', '" + TextBox6.Text + "', '" + TextBox7.Text + "', '" + TextBox8.Text + "')", cn);
OdbcCommand sc = new OdbcCommand("SELECT LAST_INSERT_ID()", cn);
//convert LAST INSERT into string theUserId
string filenameDB = Path.GetFileName(FileUpload1.FileName);
string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(fileuploadpath);
string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") + filenameDB;
Label10.Text = "Upload status: File uploaded!";
OdbcCommand cm = new OdbcCommand("INSERT INTO Pictures (picturepath, UserId) VALUES ('" + fileuploadpaths + "', " + theUserId + ")", cn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Label10.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
//e.Authenticated = true;
//Response.Redirect("Login.aspx");
// Event useradded is true forward to login
}
}
}
您需要从用户插入返回新的用户id。从mysql自动增量文档: 您可以检索最新的 使用 最后一个\u INSERT\u ID()SQL函数或 mysql_insert_id()C API函数。 这些功能是 连接特定,因此它们返回 值不受其他值的影响 正在执行的连接 插入
无论如何,您需要存储此返回并将其传递到相关操作中。我看不到您的表结构,但它是否像插入用户表、检索用户ID、保留用户ID那样简单(可能通过查询字符串传递到上载页,或使用会话等),然后在图片表insert中使用该用户ID?H.如果用户的图片比例为1:1,是否可以将图片路径放入用户表中
如果没有,MySQL有一个last_insert_id()函数,允许您从表(在本例中为用户)中获取最后一个自动增量值-通常是主键。不,我无法将它们放入用户表中,如何使用last_insert方法将我的insert合并到用户和图片表中?如果您首先执行用户插入,然后在插入图片之前获取最后一个插入id
选择LAST\u INSERT\u ID()
确保在与INSERT相同的数据库连接中运行此操作。因此,从User中选择LAST\u INSERT\u ID(),或从User中选择LAST\u USERID()?无需指定表。只需选择最后一个插入ID()