在sql中更新表

时间:2012-07-24 08:41:32

标签: c# sql

ALTER PROCEDURE InsertHash
    @FileName varchar(max),
    @Hash  varchar(max)
AS
UPDATE tabletest 
SET deneme1 = @Hash, deneme2 =@FileName

这是我的存储过程,我发送了一些数据

while (rdr.Read())
{
    string filename = @"\\" + rdr.GetString(3);
    filename = System.IO.Path.Combine(filename, rdr.GetString(2));
    filename = System.IO.Path.Combine(filename, rdr.GetString(1));
    computeHashh1 abc = new computeHashh1();
    Console.WriteLine(abc.computeHash(filename));
    SqlCommand myCommand2 = new SqlCommand("InsertHash", myConnection);
    myCommand2.CommandType = CommandType.StoredProcedure;
    SqlParameter param = new SqlParameter("@FileName", filename);
    myCommand2.Parameters.Add(param);
    SqlParameter param2 = new SqlParameter("@Hash", abc.computeHash(filename));
    myCommand2.Parameters.Add(param2);
}

当我运行代码时它没有更新表,可能是什么问题,sory对于这样的坏问题我对sql来说相当新鲜

2 个答案:

答案 0 :(得分:6)

myCommand2的一件事是......执行它:

myCommand2.ExecuteNonQuery();
嗯,实际上你也应该处理它......

while (rdr.Read())
{
    // ... blah
    using(var myCommand2 = new SqlCommand("InsertHash", myConnection))
    {
        // setup parameters etc
        myCommand2.ExecuteNonQuery();
    }
}

或者,如果这看起来太多了......一些“小巧”的爱:

myConnection.Execute("InsertHash", new {
        FileName = filename,
        Hash = abc.computeHash(filename)
    }, commandType: CommandType.StoredProcedure);

答案 1 :(得分:3)

您尚未执行命令:

myCommand2.ExecuteNonQuery()