主题:急待求解!c#数据不能更新问题
为什么数据库不能随着textbox添加的数据一起更新呢,从新启动后数据库还是原来的样子,不能更新,添加啊,急啊,请大侠帮助一下[em14][em14]下面是我写的代码
private void butADD_Click(object sender, EventArgs e)
{/*
string connnectionString =@" Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database\SuperMarket.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connnectionString);
conn.Close();
StringBuilder str = new StringBuilder();
str.Append("INSERT INTO GoodsTable(商品编号,商品名称,供应价格,供应商,供应日期)");
str.Append("VALUES('" + Convert.ToInt32(gnubtextBox.Text.ToString()) + "','" + gnamtextBox.ToString() + "','" + Convert.ToSingle(gpritextBox.Text.ToString()) + "','" + gmaktextBox.Text.ToString() + "','" + Convert.ToDateTime(gdattextBox.Text) + "')");
SqlCommand cmd = new SqlCommand(str.ToString(), conn);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("OK");
conn.Close();
*/
这是第二种写法,也是不能实现,求解啊!!!!
try
{
string connnectionString = @" Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database\SuperMarket.mdf;Integrated Security=True;User Instance=True";
string insert = "INSERT INTO GoodsTable(商品名称, 商品编号, 供应价格, 供应商, 供应日期) VALUES ('" + gnamtextBox.Text + "', " + Convert.ToInt32(gnubtextBox.Text.ToString()) + ", " + Convert.ToSingle(gpritextBox.Text.ToString()) + ", '" + gmaktextBox.Text + "', " + gdattextBox.Text + ")";
SqlConnection conn = new SqlConnection(connnectionString);
conn.Open();
using (SqlCommand cmd = new SqlCommand(insert, conn))
{
cmd.ExecuteNonQuery();
cmd.Dispose();
}
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void butADD_Click(object sender, EventArgs e)
{/*
string connnectionString =@" Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database\SuperMarket.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connnectionString);
conn.Close();
StringBuilder str = new StringBuilder();
str.Append("INSERT INTO GoodsTable(商品编号,商品名称,供应价格,供应商,供应日期)");
str.Append("VALUES('" + Convert.ToInt32(gnubtextBox.Text.ToString()) + "','" + gnamtextBox.ToString() + "','" + Convert.ToSingle(gpritextBox.Text.ToString()) + "','" + gmaktextBox.Text.ToString() + "','" + Convert.ToDateTime(gdattextBox.Text) + "')");
SqlCommand cmd = new SqlCommand(str.ToString(), conn);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("OK");
conn.Close();
*/
这是第二种写法,也是不能实现,求解啊!!!!
try
{
string connnectionString = @" Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\database\SuperMarket.mdf;Integrated Security=True;User Instance=True";
string insert = "INSERT INTO GoodsTable(商品名称, 商品编号, 供应价格, 供应商, 供应日期) VALUES ('" + gnamtextBox.Text + "', " + Convert.ToInt32(gnubtextBox.Text.ToString()) + ", " + Convert.ToSingle(gpritextBox.Text.ToString()) + ", '" + gmaktextBox.Text + "', " + gdattextBox.Text + ")";
SqlConnection conn = new SqlConnection(connnectionString);
conn.Open();
using (SqlCommand cmd = new SqlCommand(insert, conn))
{
cmd.ExecuteNonQuery();
cmd.Dispose();
}
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}