如何使用c#在asp.net中使用bulk执行insert查询?

时间:2014-01-29 19:01:41

标签: c# asp.net sql sql-server

我想在SQL Server 2008中将ImageName,ImageData,Description,CategoryId,Price,DeescriptionFilePath插入ImageTable

 protected void btnSubmit_Click(object sender, EventArgs e)
 {
        string ImageData = Server.MapPath(FileUploadImage.FileName);
        int Category = int.Parse(ddlCategory.Text);
        string ImageTitle = txtImageTitle.Text;
        string Description = txtDescription.Text;
        int Price = int.Parse(txtPrice.Text);
        string DescriptionFilePath = Server.MapPath(FileUploadDescription.FileName);

        //create sqlconnection
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);

        //open the connection
        con.Open();

        //create sql command
        SqlCommand cmdInsert = new SqlCommand();
        cmdInsert.Connection = con;
        cmdInsert.CommandType = CommandType.Text;
        cmdInsert.CommandText = "INSERT INTO ImageTable(ImageName ,ImageData, Description, CategoryId, Price, DeescriptionFilePath) Values ('" + ImageTitle + "',(SELECT * FROM OPENROWSET(BULK N'" + ImageData + "',SINGLE_BLOB) AS Image),'" + Description + "', '" + Category + "','" + Price + "','" + DescriptionFilePath + "')";

        int rows = cmdInsert.ExecuteNonQuery();

        if (rows == 1)
        {
            lblMsg.Text = "Item upload Successfully!";
        }
        else
        {
            lblMsg.Text = "Error in uploading item. Try again!";
        }

        con.Close();
}

ExecuteNonQuery()方法发生错误,因为我用来批量存储图片。

cmdInsert.CommandText = "INSERT INTO ImageTable(ImageName ,ImageData, Description, CategoryId, Price, DeescriptionFilePath) Values ('" + ImageTitle + "',(SELECT * FROM OPENROWSET(BULK N'" + ImageData + "',SINGLE_BLOB) AS Image),'" + Description + "', '" + Category + "','" + Price + "','" + DescriptionFilePath + "')";

那么如何使用c#在asp.net中使用bulk执行insert查询?

0 个答案:

没有答案