winform项目操作数据库的update可以MessageBox到更新的数据,查看数据库却没有变化。
全部关闭后再调试,数据库还是可以记住上次的更新值。这太神奇了吧?if(openFileDialog1.ShowDialog()==DialogResult.OK){usi...
全部关闭后再调试,数据库还是可以记住上次的更新值。这太神奇了吧?
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
using (FileStream filestream = File.OpenRead(openFileDialog1.FileName))
{
using (StreamReader streamReader = new StreamReader(filestream))
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\DRSJ.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert into T_DRSJ(Name,Age) values(@name,@age)";
string line = null;
while ((line = streamReader.ReadLine()) != null)
{
string[] strs = line.Split('|');
string names = strs[0];
int ages = Convert.ToInt32(strs[1]);
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("name", names));
cmd.Parameters.Add(new SqlParameter("age", ages));
cmd.ExecuteNonQuery(); 展开
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
using (FileStream filestream = File.OpenRead(openFileDialog1.FileName))
{
using (StreamReader streamReader = new StreamReader(filestream))
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\DRSJ.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert into T_DRSJ(Name,Age) values(@name,@age)";
string line = null;
while ((line = streamReader.ReadLine()) != null)
{
string[] strs = line.Split('|');
string names = strs[0];
int ages = Convert.ToInt32(strs[1]);
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("name", names));
cmd.Parameters.Add(new SqlParameter("age", ages));
cmd.ExecuteNonQuery(); 展开
2个回答
展开全部
/// <summary>
/// 增加一条数据
/// </summary>
public int Add(Maticsoft.Model.Accounts_PermissionCategories model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into Accounts_PermissionCategories(");
strSql.Append("Description)");
strSql.Append(" values (");
strSql.Append("@Description)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@Description", SqlDbType.VarChar,255)};
parameters[0].Value = model.Description;
object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
if (obj == null)
{
return 0;
}
else
{
return Convert.ToInt32(obj);
}
}
/// 增加一条数据
/// </summary>
public int Add(Maticsoft.Model.Accounts_PermissionCategories model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into Accounts_PermissionCategories(");
strSql.Append("Description)");
strSql.Append(" values (");
strSql.Append("@Description)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@Description", SqlDbType.VarChar,255)};
parameters[0].Value = model.Description;
object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
if (obj == null)
{
return 0;
}
else
{
return Convert.ToInt32(obj);
}
}
追问
我的语句没有错误,不能修改数据库是因为在winfrom中的program中添加一段代码才能操作数据库,关键是我添加了,但是还是不行。
追答
换个执行方式,如果你是添加多条数据么就改用事务就可以了吧:
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\DRSJ.mdf;Integrated Security=True;User Instance=True"))
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string sql = "Insert into T_DRSJ(Name,Age) values("+names+","+ages+")";
SqlCommand cmd = new SqlCommand(sql, conn);
if(null!=cmd.ExecuteNonQuery())
{
Int i=cmd.ExecuteNonQuery();
if(i>0)
{
MessageBox.Show("操作成功!");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询