我正在尝试从表单中读取数据并在将媒体添加到其他表时更新一个表,但是一旦执行更新查询,连接就会关闭。如何保持打开并运行第二个表的插入查询?
try
{
dbConn.Open();
OleDbAdapter = new OleDbDataAdapter(cmd1, dbConn);
OleDbAdapter.SelectCommand.Parameters.Add("@buttonClicked", OleDbType.Integer).Value = archiveIdNumber.InnerText.Substring(33);
OleDbAdapter.Fill(infoDs, "First Table");
OleDbAdapter.SelectCommand.CommandText = cmd2;
OleDbAdapter.Fill(infoDs, "Second Table");
OleDbAdapter.Dispose();
string cmdString = "Update ARCHIVE_DECADE_TBL Set PRODUCT_NAME='" + Request.Form["nameBox"] + "', MODEL_NUMBER='" + Request.Form["modelBox"] + "', YEAR_INTRODUCED='" + Request.Form["startBox"] + "', YEAR_DISCONTINUED='" + Request.Form["endBox"] + "', PRODUCT_LINE='" + Request.Form["lineBox"] + "', LOCATION='" + Request.Form["locationBox"] + "', QUANTITY='" + Request.Form["quantityBox"] + "' " +
"Where ARCHIVE_ID_NUMBER=" + int.Parse(Request.Form["archiveBox"]);
OleDbAdapter.UpdateCommand = new OleDbCommand(cmdString, dbConn);
OleDbAdapter.UpdateCommand.ExecuteNonQuery();
if (addPhotos.HasFiles)
{
//cmdString = "Update ARCHIVE_DECADE_TBL Set PRODUCT_NAME='" + Request.Form["nameBox"] + "', MODEL_NUMBER='" + Request.Form["modelBox"] + "', YEAR_INTRODUCED='" + Request.Form["startBox"] + "', YEAR_DISCONTINUED='" + Request.Form["endBox"] + "', PRODUCT_LINE='" + Request.Form["lineBox"] + "', LOCATION='" + Request.Form["locationBox"] + "' " +
//"Where ARCHIVE_ID_NUMBER=" + int.Parse(Request.Form["archiveBox"]);
var x = 0;
if (!System.IO.Directory.Exists(Server.MapPath("includes/images/archives/" + Request.Form["archiveBox"] + "_1")))
{
System.IO.Directory.CreateDirectory(Server.MapPath("includes/images/archives/" + Request.Form["archiveBox"] + "_1"));
}
String filePath = Server.MapPath("includes/images/archives/" + Request.Form["archiveBox"] + "_1");
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
if (userPostedFile.ContentLength > 0)
{
string extension = Path.GetExtension(userPostedFile.FileName);
//Request.Form["archiveBox"] + "_" + i will be the same as the "IMAGE" column in the IMAGE_TBL
uploadedFiles[i].SaveAs(filePath + "/" + Request.Form["archiveBox"] + "_" + (i + 1) + extension);
}
x++;
}
cmdString = "INSERT INTO ARCHIVE_IMAGE_TBL (ARCHIVE_ID, MODEL_NUMBER, LOCATION, IMAGE, NUMBER_OF_IMAGES, IMAGE_FILE_TYPE) VALUES ('" + Request.Form["archiveBox"] + "', '" + Request.Form["modelBox"] + "', '" + Request.Form["locationBox"] + "', '" + Request.Form["archiveBox"] + "_1" + "', '" + uploadedFiles.Count + "', '.jpg');";
OleDbAdapter.InsertCommand = new OleDbCommand(cmdString, dbConn);
OleDbAdapter.InsertCommand.ExecuteNonQuery();
}
dbConn.Close();
}
答案 0 :(得分:0)
我建议使用程序:
CREATE PROCEDURE [MeProcedure]
AS
BEGIN
--procedure content
END
或者您可以尝试连接查询:
UPDATE tbl SET [a]=1, [b]='c' WHERE [d]=@param; INSERT INTO tbl_2( [a], [b] ) VALUES ( 3, 'g' ) WHERE [d]=@param;
这可以使用SqlCommand对象完成,如下所示:
SqlCommand cmd = new SqlCommand(@"UPDATE tbl SET [a]=1, [b]='c' WHERE [d]=@param; INSERT INTO tbl_2( [a], [b] ) VALUES ( 3, 'g' ) WHERE [d]=@param;");
cmd.Parameters["param"].Value = 5;
var result = cmd.ExecuteNonQuery();
编辑: 来自问题的查询示例:
string cmdText = "Update ARCHIVE_DECADE_TBL Set PRODUCT_NAME=@pName, MODEL_NUMBER=@mNumber, YEAR_INTRODUCED=@yearInt, YEAR_DISCONTINUED=@yearDisc, PRODUCT_LINE=@pLine, LOCATION=@loc, QUANTITY=@quantity Where ARCHIVE_ID_NUMBER=@archive";
您可以在此处添加其他查询,如:
cmdText += ";SELECT COUNT(*) FROM ARCHIVE_DECADE_TBL";
然后你可以分配SqlCommand及其参数:
OleDbCommand cmd = new OleDbCommand(cmdText,dbConn);
cmd.Parameters.Add(new OleDbParameter("pName", DbType=DbType.VarChar, 12));
cmd.Parameters.Add(new OleDbParameter("archive", DbType=DbType.Integer){ Value = int.Parse(Request.Form["archiveBox"] });
// more parameters here
cmd.ExecuteNonQuery();
答案 1 :(得分:0)
您致电:
OleDbAdapter.Dispose();
在设置之后,我很惊讶这种情况有效。
在完成对象之前,请勿对其进行Dispose()。