2014-09-01 70 views
1

我想获得这些select语句的值,则插入该值到另一个表如何分配变量SELECT语句结果C#

if (FileUpload1.HasFile) 
{ 
    strsq = "select max(device_id) from device"; 
    string str = FileUpload1.FileName; 
    FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//uploads//" + str); 
    string path = "~/device/uploads/" + str.ToString(); 
    SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["paetdataConnectionString1"].ConnectionString); 
    con1.Open(); 
    string cmdStr1 = "insert into [dev_img] (strsq,image) values (@id,@path)"; 
    SqlCommand com = new SqlCommand(cmdStr1,con1); 
    com.Parameters.AddWithValue("@id", strsq); 
    com.Parameters.AddWithValue("@path", path); 
    com.ExecuteNonQuery(); 
    con1.Close(); 
} 

回答

1
strsq = "select max(device_id)as DevID from device"; 
    //To get the Device ID 
    int DevID; 
    int.TryParse(dSet.Tables[dTableName].Rows[0]["DevID"].ToString(), out DevID); 

    //Assign the value 
    com.Parameters.Add("@id", SqlDbType.Int); 
    com.Parameters["@id"].Value = DevID; 

此外,还要确保您在使用使用条款你的连接和命令,所以它会关闭连接! :)