使用C#中的会话参数更新SQL数据库

时间:2013-08-07 12:37:36

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

我正在寻找一种UPDATE来自C code-behind的{​​{1}} SQL服务器数据库session parameter的方法,这就是我的SqlCommand ASP:

重要提示: C#中的SqlCommand将在static function中,是否会出现问题?

提前致谢!

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SPMS_DBConnectionString1 %>" 
        UpdateCommand="UPDATE Project SET startDate = @startDate, dueDate = @dueDate, progress = @progress, status = @status WHERE (prID = @prID)">
        <UpdateParameters>
            <asp:SessionParameter Name="prID" SessionField="project" Type="String" />
            <asp:Parameter Name="startDate" Type="DateTime"/>
            <asp:Parameter Name="dueDate" Type="DateTime" />
            <asp:Parameter Name="progress" Type="Int32" />
            <asp:Parameter Name="status" Type="Boolean" />
            <asp:Parameter Name="projectID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

这就是我解决它的方法:

    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SPMS_DBConnectionString1"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(connectionString))
    using (SqlCommand command = connection.CreateCommand())
    { 

        DateTime startDate = DateFromMiliSec(start);
        DateTime dueDate = DateFromMiliSec(end);
        int progress = Convert.ToInt32(prog);
        string prID = HttpContext.Current.Session["project"].ToString();         

        command.CommandText ="UPDATE Project SET startDate = @startDate, dueDate = @dueDate, progress = @progress, status = @status WHERE (prID = @prID)";

        command.Parameters.AddWithValue("@startDate", startDate );
        command.Parameters.AddWithValue("@dueDate", dueDate );
        command.Parameters.AddWithValue("@progress", progress );
        command.Parameters.AddWithValue("@status", status );
        command.Parameters.AddWithValue("@prID", prID );

        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
    }