使用asp.net C#升级SQL Server数据库 [英] updating sql server database using asp.net c#

查看:90
本文介绍了使用asp.net C#升级SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯具有一个WebForm更新我的数据库问题。我用了一系列文本框和dropdownlists这些填充的数据源,但由于某种原因,我不能更新它们。

im having issues updating my database from a webform. I use a series of textboxes and dropdownlists which are populated by the datasource but for some reason i cant update them.

基本上是一个行的主键被传递到下一个页面,文本框,下拉列表填充了该行的数据。

Basically the primary key of a row is passed into the next page where the textboxes,drop down lists are populated with the data from that row.

下面是我试过到目前为止

Here is what i have tried so far

    //Update the record with new information entered into controls
    protected void UpdateBut_Click(object sender, EventArgs e)
    {

        using (SqlConnection conn = new SqlConnection("Data Source=stephenp\\sqlexpress;Initial Catalog=Asset management System DB;Integrated Security=True"))
        {
            string sql = "UPDATE Peripherals SET PeripheralType=@PeripheralType, Model=@Model, PeripheralSerialNumber=@SerialNum, " +
                "Company = @Company, Department=@Department, Status=@Status, WarrantyExpirationDate=@Warranty, PeripheralCapexNumber=@CapexNum, " +
                "IPAddress=@IPAddress, LastModifiedDate = @LastMD, LastModifiedBy=@LastMB WHERE (PeripheralTagNumber = @PeripheralTagNumber)";


          //try
           //{
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                cmd.Parameters.AddWithValue("@PeripheralType", TypeDDL.Text.Trim());
                cmd.Parameters.AddWithValue("@Model", ModelTB.Text.Trim());
                cmd.Parameters.AddWithValue("@SerialNum", SerialNumTB.Text.Trim());
                cmd.Parameters.AddWithValue("@Company", CompanyDDL.Text.Trim());
                cmd.Parameters.AddWithValue("@Department", DepartmentDDL.Text.Trim());
                cmd.Parameters.AddWithValue("@Status", StatusDDL.Text.Trim());
                cmd.Parameters.AddWithValue("@Warranty", DateTime.Parse(WarrantyTB.Text.Trim()).ToShortDateString());
                cmd.Parameters.AddWithValue("@CapexNum", CapexNumTB.Text.Trim());
                cmd.Parameters.AddWithValue("@IPAddress", IPAddressTB.Text.Trim());
                cmd.Parameters.AddWithValue("@LastMD", DateTime.Now.Date.ToShortDateString());
                cmd.Parameters.AddWithValue("@LastMB", Session["username"].ToString());
                cmd.Parameters.AddWithValue("@PeripheralTagNumber", ID);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
             }
           //}
            //catch (SqlException ex) { }
        }

        this.getData();
    }

感谢

推荐答案

使用AddWithValue可能会导致一些微妙的错误。例如,如果你列LastModifiedDate为datetime型的你应该传递一个DateTime值作为第二个参数的AddWithValue,而不是一个字符串。 同为WarrantyDate

Using AddWithValue could lead to some subtle bugs. For example if your column LastModifiedDate is of datetime type you should pass a DateTime value as the second parameter in AddWithValue, not a string. The same for the WarrantyDate

您可以尝试改变这些行

 cmd.Parameters.AddWithValue("@LastMD", DateTime.Now.Date);
 cmd.Parameters.AddWithValue("@Warranty", DateTime.Parse(WarrantyTB.Text.Trim()));

这篇关于使用asp.net C#升级SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆