我正在使用asp.net 4.5和SQL Server 2008 Express。
我想将一个表单的数据插入到我的数据库中,连接字符串是正常的并且在另一个页面中工作,但是在这个页面中,似乎插入按钮只是刷新页面而没有发生插入!
代码:
protected void Button1_Click(object sender, EventArgs e)
{
var conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
var cmdd = "insert into poroje (Pname,Pmozu,Ppayan,StartDate,EndTime,makan,tozih) values(@Pname,@Pmozu,@Ppayan,@StartDate,@EndTime,@makan,@tozih)";
using (SqlConnection cnn = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand(cmdd, cnn))
{
if (aksporoje.HasFile)
{
if (CheckFileType(aksporoje.FileName))
{
PersianCalendar Pe = new PersianCalendar();
string s = Pe.GetYear(System.DateTime.Now).ToString();
if (Directory.Exists("~/poroje/"+s))
{
Directory.CreateDirectory(Server.MapPath("~/poroje/" + s+pushePoroje(System.DateTime.Today)));
string filePath = "~/poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName;
aksporoje.SaveAs(MapPath(filePath));
cmd.Parameters.AddWithValue("@aks","poroje/"+ s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName);
}
else {
Directory.CreateDirectory(Server.MapPath("~/poroje/"+s));
Directory.CreateDirectory(Server.MapPath("~/poroje/"+s+ pushePoroje(System.DateTime.Today)));
string filePath = "~/poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName;
aksporoje.SaveAs(MapPath(filePath));
cmd.Parameters.AddWithValue("@aks", "poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName);
}
}
}
cmd.Parameters.AddWithValue("@Pname", txtnam.Text);
cmd.Parameters.AddWithValue("@Pmozu", txtmozu.Text);
cmd.Parameters.AddWithValue("@Ppayan",false);
cmd.Parameters.AddWithValue("@StartDate",PersianDateTextBox1.Text);
cmd.Parameters.AddWithValue("@EndTime",null);
cmd.Parameters.AddWithValue("@makan", txtmakan.Text);
cmd.Parameters.AddWithValue("@tozih", txttozih.Text);
try
{
cnn.Open();
int recordsAffected = cmd.ExecuteNonQuery();
}
catch (SqlException)
{
lblpeygham.Visible = true;
lblpeygham.Text = "error";
}
finally
{
cnn.Close();
lblpeygham.Visible = true;
lblpeygham.Text = "data inserted";
}
}
}
当我点击按钮时,页面刷新,并向我显示“已插入数据”消息。但是当我检查数据库时没有插入任何内容!你能帮我吗?
答案 0 :(得分:0)
在实例化SQL COMMAND对象之前需要打开连接。
我打赌你cnn.Open正在消除任何设置。
查看此帖子,了解最终的正确工作代码。这是正确的事件顺序。
1 - Open connection object
2 - Declare string
3 - Open Command object
4 - Set parameters
5 - Call Execute Non SQL
6 - Close command object
7 - Close Connection object
ASP.Net insert data from Textbox to a database
另外,在代码中加一个断点,检查返回值。
祝你好运Ĵ
答案 1 :(得分:0)
将整个SQL命令放入一个直接在服务器上运行的字符串中。怎么了?增加值了吗?如果是,那么再次检查代码。这应该会对你有所帮助。
答案 2 :(得分:0)
我认为这是错误的。
int recordsAffected = cmd.ExecuteNonQuery();
我以前从来没有从int运行命令executeNonquery。它应该像
cmd.ExecuteNonQuery();
即便如此,我认为你想要尝试抓住所有东西......就像这样..
try
{
cnn.Open();
var cmdd = "insert into poroje (Pname,Pmozu,Ppayan,StartDate,EndTime,makan,tozih) values(@Pname,@Pmozu,@Ppayan,@StartDate,@EndTime,@makan,@tozih)";
using (SqlConnection cnn = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand(cmdd, cnn))
{
if (aksporoje.HasFile)
{
if (CheckFileType(aksporoje.FileName))
{
PersianCalendar Pe = new PersianCalendar();
string s = Pe.GetYear(System.DateTime.Now).ToString();
if (Directory.Exists("~/poroje/"+s))
{
Directory.CreateDirectory(Server.MapPath("~/poroje/" + s+pushePoroje(System.DateTime.Today)));
string filePath = "~/poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName;
aksporoje.SaveAs(MapPath(filePath));
cmd.Parameters.AddWithValue("@aks","poroje/"+ s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName);
}
else {
Directory.CreateDirectory(Server.MapPath("~/poroje/"+s));
Directory.CreateDirectory(Server.MapPath("~/poroje/"+s+ pushePoroje(System.DateTime.Today)));
string filePath = "~/poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName;
aksporoje.SaveAs(MapPath(filePath));
cmd.Parameters.AddWithValue("@aks", "poroje/" + s + "/" + pushePoroje(System.DateTime.Today) + "/" + aksporoje.FileName);
}
}
}
cmd.Parameters.AddWithValue("@Pname", txtnam.Text);
cmd.Parameters.AddWithValue("@Pmozu", txtmozu.Text);
cmd.Parameters.AddWithValue("@Ppayan",false);
cmd.Parameters.AddWithValue("@StartDate",PersianDateTextBox1.Text);
cmd.Parameters.AddWithValue("@EndTime",null);
cmd.Parameters.AddWithValue("@makan", txtmakan.Text);
cmd.Parameters.AddWithValue("@tozih", txttozih.Text);
cmd.ExecuteNonQuery();
}
catch (SqlException)
{
lblpeygham.Visible = true;
lblpeygham.Text = "error";
}
finally
{
cnn.Close();
lblpeygham.Visible = true;
lblpeygham.Text = "data inserted";
}
这样的事情应该有用。但我认为主要问题是上面提到的第一个问题。
希望这有帮助。
答案 3 :(得分:0)
catch (SqlException)
{
lblpeygham.Visible = true;
lblpeygham.Text = "error";
}
到
catch (SqlException ex)
{
lblpeygham.Visible = true;
lblpeygham.Text = ex.message;
}
并了解数据库中的startDate类型是dateTime但我在其中插入了nvarchar! 但现在我有一个新问题。我想在主机中创建带有年份名称的新文件夹,但如果我写
Directory.CreateDirector("~/poroje/"+s);
发生访问拒绝错误。如果我使用
Directory.CreateDirectory(Server.MapPath("~/poroje/"+s));
没有错误,但目录没有创建!