0

这是使用 c# 将日期时间插入 sql server 2008 的代码。相同的代码在我的本地系统中运行良好,但有时无法在服务器中运行。请参阅下面提到的代码,让我知道您的答案和建议

protected void Button1_Click(object sender, EventArgs e)
{
    string ad;
    btnupdate.Visible = false;
    btnactive1.Visible = false;
    if (FileUpload1.HasFile)
    {
        //getting length of uploaded file
        int length = FileUpload1.PostedFile.ContentLength;
        //create a byte array to store the binary image data
        byte[] imgbyte = new byte[length];
        //store the currently selected file in memeory
        HttpPostedFile img = FileUpload1.PostedFile;
        //set the binary data
        string area = ddlarea.SelectedItem.Text;
        string cat = ddlcategory.SelectedItem.Text;
        string subcat = ddlsubcategory.SelectedItem.Text;
        string head = txthead.Text;
        string subhead = txtsubhead.Text;
        string shortdesc = txtshortdesc.Text;
        string url = txturl.Text;
        //string imagename = txtimagename.Text;
        string regdate = txtregdate.Text;
        string expirydate = txtexpirydate.Text;
        string customername = txtcustomername.Text;
        string contact = txtcontact.Text;
        string email = txtemail.Text;
        if (CheckBox2.Checked == true)
        {
            ad = "paid ad";
        }
        else
        {
            ad = "free ad";
        }
        img.InputStream.Read(imgbyte, 0, length);
        con = new SqlConnection(str);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into tbl_paid_ads (area,catagory,subcatagory,heading,subheading,shortdescription,url,imagedata,dateofregistration,dateofexpiry,customername,contactno,email,ad) values (@area,@cat,@subcat,@head,@subhead,@shortdesc,@url,@imagedata,@regdate,@expirydate,@customername,@contact,@email,@ad)", con);
        //SqlCommand cmd = new SqlCommand("insert into tbl_paid_ads (image_name,image) values (@imagename,@imagedata)", con);
        cmd.Parameters.Add("@area", SqlDbType.NVarChar, 50).Value = area;
        cmd.Parameters.Add("@cat", SqlDbType.NVarChar, 50).Value = cat;
        cmd.Parameters.Add("@subcat", SqlDbType.NVarChar, 50).Value = subcat;
        cmd.Parameters.Add("@head", SqlDbType.NVarChar, 50).Value = head;
        cmd.Parameters.Add("@subhead", SqlDbType.NVarChar, 100).Value = subhead;
        cmd.Parameters.Add("@shortdesc", SqlDbType.NVarChar, 1000).Value = shortdesc;
        cmd.Parameters.Add("@url", SqlDbType.NVarChar, 50).Value = url;
        //cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename;
        cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;
        cmd.Parameters.Add("@regdate", SqlDbType.DateTime).Value = regdate;
        cmd.Parameters.Add("@expirydate", SqlDbType.DateTime).Value = expirydate;
        cmd.Parameters.Add("@customername", SqlDbType.NVarChar, 50).Value = customername;
        cmd.Parameters.Add("@contact", SqlDbType.NVarChar, 50).Value = contact;
        cmd.Parameters.Add("@email", SqlDbType.NVarChar, 50).Value = email;
        cmd.Parameters.Add("@ad", SqlDbType.NVarChar, 50).Value = ad;
        int count = cmd.ExecuteNonQuery();
        con.Close();
        if (count == 1)
        {
            Response.Write("<script>alert('Paid Ad added successfully')</script>");
        }
        resetpaidad();
    }
}
4

3 回答 3

1

用户界面 (UI) 应验证日期是否采用特定格式,然后您需要将其转换stringDateTime. 例如,如果 UI 日期格式是DD/MM/YYYY您应该替换此行...

string expirydate = txtexpirydate.Text;

和...

DateTime expirydate = DateTime.ParseExact(txtexpirydate.Text, "dd/MM/yyyy", 
                                          CultureInfo.InvariantCulture);

对于regdate.

注意:如果文本框的日期格式始终是固定的,上述方法将起作用。但是,如果 UI 验证在不同位置强制执行不同的格式,您将需要获取 UI 当前使用的日期格式,这可能取决于 UI 运行的区域设置。

于 2013-05-11T10:08:01.527 回答
0

避免这种情况的方法是设置参数的 SqlDbType,并且在分配参数值时,客户端值强制转换为映射到 SqlDbType 的 CLR 类型。

于 2013-05-11T11:13:15.707 回答
0

代替:

string regdate = txtregdate.Text;
string expirydate = txtexpirydate.Text;

尝试这个:

Datetime regdate =  DateTime.ParseExact(txtregdate.Text, "M-d-yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime expirydate =  DateTime.ParseExact( txtexpirydate.Text, "M-d-yyyy", System.Globalization.CultureInfo.InvariantCulture);

假设您的文本框包含格式为 Md-yyyy 的日期,否则请更改它。

于 2013-05-11T10:07:53.437 回答