0

我正在寻找一种从C# 中使用 a访问UPDATESQL Server 数据库的方法,这就是我在asp中的样子:code-behindsession parameterSqlCommand

重要提示:SqlCommandC# 中会出现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>
4

1 回答 1

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();
    } 
于 2013-08-07T16:12:31.623 回答