这是使用 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();
}
}