's'附近的语法不正确。字符串')'后面的未闭合引号

时间:2015-07-27 16:13:01

标签: sql-server asp.net-mvc-5

我知道我应该使用参数化查询,但它是一个插入文本文件的内部工具。有人看到Unclosed报价的错误吗?奇怪的是,我在SQL Server 2014中运行它,然后它成功运行,然后我删除了“和值分配的结尾处。但是在我的项目的Visual Studio中它需要”;  这是我的代码:

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(conn);
                string query = "Insert into BluStar(ProductID,Manufacturer,SKU,BlueStarSKU,MSRP,RoughSummary,COST,Inventory,Weight,CAT4th,CAT1st,CAT2nd,CAT3rd,ProductName,Genre,ProductName2,RougherSummary,Cat_4th,Field1,Field2,Field3,Field4,Field5,Field6,Field7,Field8,Field9,Field10,Field11,Field12,Field13) Values('" + ds.Tables[0].Rows[i][0].ToString() + "','" + ds.Tables[0].Rows[i][1].ToString() + "','" + ds.Tables[0].Rows[i][2].ToString() + "','" + ds.Tables[0].Rows[i][3].ToString() + "','" + ds.Tables[0].Rows[i][4].ToString() + "','" + ds.Tables[0].Rows[i][5].ToString() + "','" + ds.Tables[0].Rows[i][6].ToString() + "','" + ds.Tables[0].Rows[i][7].ToString() + "','" + ds.Tables[0].Rows[i][8].ToString() + "','" + ds.Tables[0].Rows[i][9].ToString() + "','" + ds.Tables[0].Rows[i][10].ToString() + "','" + ds.Tables[0].Rows[i][11].ToString() + "','" + ds.Tables[0].Rows[i][12].ToString() + "','" + ds.Tables[0].Rows[i][13].ToString() + "','" + ds.Tables[0].Rows[i][14].ToString() + "','" + ds.Tables[0].Rows[i][15].ToString() +
                "','" + ds.Tables[0].Rows[i][16].ToString() + "','" + ds.Tables[0].Rows[i][17].ToString() + "','" + ds.Tables[0].Rows[i][18].ToString() + "','" + ds.Tables[0].Rows[i][19].ToString() + "','" + ds.Tables[0].Rows[i][20].ToString() + "','" + ds.Tables[0].Rows[i][21].ToString() + "','" + ds.Tables[0].Rows[i][22].ToString() + "','" + ds.Tables[0].Rows[i][23].ToString() + "','" + ds.Tables[0].Rows[i][24].ToString() + "','" + ds.Tables[0].Rows[i][25].ToString() + "','" + ds.Tables[0].Rows[i][26].ToString() + "','" + ds.Tables[0].Rows[i][27].ToString() + "','" + ds.Tables[0].Rows[i][28].ToString() + "','" + ds.Tables[0].Rows[i][29].ToString() + "','" + ds.Tables[0].Rows[i][30].ToString() + "')";
                con.Open();
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.ExecuteNonQuery();
                con.Close();
            }

以下是我必须从控制台转换为MVC以读取csv文件的代码。

    <%@ Page Language="C#" %>
<html>
<head>
   <title>read file insert into data base</title>
</head>
<body>
<%@Import namespace=System.Data %>
<%@Import namespace=System.Data.SqlClient %>
<% 

System.IO.StreamReader sr = new System.IO.StreamReader("/testdata/test.csv");
string line;
while(sr.Peek() != -1)
{
   line = sr.ReadLine();
   String[] parts = line.Split('\t');
   Response.Write(Server.HtmlEncode(parts[3]) + "<br/>");

string conn = "";
string Manu;
string sku;
Decimal msrp = Convert.ToDecimal(parts[4]);  
conn = ConfigurationManager.ConnectionStrings["Conn"].ToString(); 
SqlConnection objsqlconn = new SqlConnection(conn); 
objsqlconn.Open(); 
SqlCommand objcmd = new SqlCommand("Insert into ScanSource(Manufacture,SKU,MSRP) Values(@Manu, @sku,@msrp)", objsqlconn);
objcmd.Parameters.AddWithValue("@Manu",parts[1]);
objcmd.Parameters.AddWithValue("@sku",parts[3]);
objcmd.Parameters.AddWithValue("@msrp",msrp);
objcmd.ExecuteNonQuery(); 
}
%>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

Your tables need some serious help too. You have things like Field1, Field2...this is awful. Consider what happens when you need to add Field14. You will have to update everything everywhere. This kind of thing belongs in a child table.

I am NOT going to post a full solution here because there are a lot of columns and I have no idea what datatypes anything is.

This should get you started.

Insert into BluStar
(
    ProductID
    , Manufacturer
    , SKU
    , BlueStarSKU
    , MSRP
    , RoughSummary
    , COST
    , Inventory
    , Weight
    , CAT4th
    , CAT1st
    , CAT2nd
    , CAT3rd
    , ProductName
    , Genre
    , ProductName2
    , RougherSummary
    , Cat_4th
    , Field1
    , Field2
    , Field3
    , Field4
    , Field5
    , Field6
    , Field7
    , Field8
    , Field9
    , Field10
    , Field11
    , Field12
    , Field13
) 
Values
(
    @ProductID
    , @Manufacturer
    , @SKU
    , @BlueStarSKU
    , @MSRP
    , @RoughSummary
    , @COST
    , @Inventory
    , @Weight
    , @CAT4th
    , @CAT1st
    , @CAT2nd
    , @CAT3rd
    , @ProductName
    , @Genre
    , @ProductName2
    , @RougherSummary
    , @Cat_4th
    , @Field1
    , @Field2
    , @Field3
    , @Field4
    , @Field5
    , @Field6
    , @Field7
    , @Field8
    , @Field9
    , @Field10
    , @Field11
    , @Field12
    , @Field13
)

cmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = int.parse(ds.Tables[0].Rows[i][0].ToString());

答案 1 :(得分:0)

Here is another alternative using a table valued parameter. Now you have to realize I can't test this so it might need a little tweaking. Also I have no idea what datatypes you have in your table so you will need to adjust those.

Before you can use a table valued parameter you have to create the type. Here is how you might go about that.

CREATE TYPE BluStar AS TABLE
(
    ProductID int
    , Manufacturer varchar(10)
    , SKU varchar(10)
    , BlueStarSKU varchar(10)
    , MSRP varchar(10)
    , RoughSummary varchar(10)
    , COST varchar(10)
    , Inventory varchar(10)
    , Weight varchar(10)
    , CAT4th varchar(10)
    , CAT1st varchar(10)
    , CAT2nd varchar(10)
    , CAT3rd varchar(10)
    , ProductName varchar(10)
    , Genre varchar(10)
    , ProductName2 varchar(10)
    , RougherSummary varchar(10)
    , Cat_4th varchar(10)
    , Field1 varchar(10)
    , Field2 varchar(10)
    , Field3 varchar(10)
    , Field4 varchar(10)
    , Field5 varchar(10)
    , Field6 varchar(10)
    , Field7 varchar(10)
    , Field8 varchar(10)
    , Field9 varchar(10)
    , Field10 varchar(10)
    , Field11 varchar(10)
    , Field12 varchar(10)
    , Field13 varchar(10)
)

Now we have our own datatype in sql server that mimics what our actual table will look like. This probably seems a bit overkill but remember we are going to be passing in a whole set of rows instead of doing a separate insert for each and every row. Here is what our procedure would look like.

create procedure BluStar_Insert
(
    @BluStar BluStar READONLY
) as

    Insert into BluStar
    (
        ProductID
        , Manufacturer
        , SKU
        , BlueStarSKU
        , MSRP
        , RoughSummary
        , COST
        , Inventory
        , Weight
        , CAT4th
        , CAT1st
        , CAT2nd
        , CAT3rd
        , ProductName
        , Genre
        , ProductName2
        , RougherSummary
        , Cat_4th
        , Field1
        , Field2
        , Field3
        , Field4
        , Field5
        , Field6
        , Field7
        , Field8
        , Field9
        , Field10
        , Field11
        , Field12
        , Field13
    ) 
    select 
        ProductID
        , Manufacturer
        , SKU
        , BlueStarSKU
        , MSRP
        , RoughSummary
        , COST
        , Inventory
        , Weight
        , CAT4th
        , CAT1st
        , CAT2nd
        , CAT3rd
        , ProductName
        , Genre
        , ProductName2
        , RougherSummary
        , Cat_4th
        , Field1
        , Field2
        , Field3
        , Field4
        , Field5
        , Field6
        , Field7
        , Field8
        , Field9
        , Field10
        , Field11
        , Field12
        , Field13
    from @BluStar

That's all fine and dandy but without an example of this from dotnet it doesn't help much. This is intended to be the ENTIRE dotnet code. No more loops.

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
    conn.Open();
    using (SqlCommand cmd = SqlCommand("BluStar_Insert", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@BluStar", ds.Tables[0]));
        cmd.ExecuteNonQuery();
    }
}