我写了一个WinForm的小工具,自己用的。开发环境是VS2010SP1+Access2007(C#)数据库。我在往一个表里存数据是,返回结果是1,但是打开Access数据库时发现里面就是没有数据,昨天弄了半夜也没弄好,急等各位大侠指教 string _工序名称 = textBoxName.Text.Trim();
if (string.IsNullOrEmpty(_工序名称))
{
MessageBox.Show("工序名称不能为空!");
return;
}
string _计件单价 = textBoxPrice.Text.Trim();
if (string.IsNullOrEmpty(_计件单价))
{
MessageBox.Show("计件单价不能为空!");
return;
}
try
{
_计件单价 = double.Parse(_计件单价).ToString();
}
catch (Exception _Exception)
{
MessageBox.Show("计件单价只能填写数字!");
return;
}
//这是sql语句
string sql = "insert into [工序表] ([ID],[工序编号],[工序名称],[计件单价]) values('"
+ ID.ToString() + "','" + _编号 + "','" + _工序名称 + "','" + _计件单价 + "')";//这个是调用的存储方法
int result = 数据操作类.添加操作(sql); if (result == 1)
{
labelResult.Text = "添加成功!";
labelResult.ForeColor = Color.LimeGreen;
生成工序编号();
textBoxName.Text = "";
textBoxPrice.Text = "";
}
else
{
labelResult.Text = "添加失败!";
labelResult.ForeColor = Color.Red;
}//这是存入数据的方法(和下面的获得数据库连接的方法都是写在另一个类中的静态方法)
public static int 添加操作(string sql)
{
OleDbConnection conn = getConn();
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
int count= cmd.ExecuteNonQuery();
conn.Close();
return count;
}//获得数据库连接的方法(在登录方法中已经成功调用过)
public static OleDbConnection getConn()
{
OleDbConnection conn = new OleDbConnection();
string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";
strConnection += @"Data Source=Database.accdb;";
strConnection += "Persist Security Info=False";
conn.ConnectionString = strConnection;
return conn;
}
if (string.IsNullOrEmpty(_工序名称))
{
MessageBox.Show("工序名称不能为空!");
return;
}
string _计件单价 = textBoxPrice.Text.Trim();
if (string.IsNullOrEmpty(_计件单价))
{
MessageBox.Show("计件单价不能为空!");
return;
}
try
{
_计件单价 = double.Parse(_计件单价).ToString();
}
catch (Exception _Exception)
{
MessageBox.Show("计件单价只能填写数字!");
return;
}
//这是sql语句
string sql = "insert into [工序表] ([ID],[工序编号],[工序名称],[计件单价]) values('"
+ ID.ToString() + "','" + _编号 + "','" + _工序名称 + "','" + _计件单价 + "')";//这个是调用的存储方法
int result = 数据操作类.添加操作(sql); if (result == 1)
{
labelResult.Text = "添加成功!";
labelResult.ForeColor = Color.LimeGreen;
生成工序编号();
textBoxName.Text = "";
textBoxPrice.Text = "";
}
else
{
labelResult.Text = "添加失败!";
labelResult.ForeColor = Color.Red;
}//这是存入数据的方法(和下面的获得数据库连接的方法都是写在另一个类中的静态方法)
public static int 添加操作(string sql)
{
OleDbConnection conn = getConn();
conn.Open();
OleDbCommand cmd = new OleDbCommand(sql, conn);
int count= cmd.ExecuteNonQuery();
conn.Close();
return count;
}//获得数据库连接的方法(在登录方法中已经成功调用过)
public static OleDbConnection getConn()
{
OleDbConnection conn = new OleDbConnection();
string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";
strConnection += @"Data Source=Database.accdb;";
strConnection += "Persist Security Info=False";
conn.ConnectionString = strConnection;
return conn;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货