- 首先设计DataTable
DataTable dt = new DataTable();
#region Datatable
public void Getdt()
{
if (dt.Columns.Count < 1)
{
DataColumn dc = new DataColumn("商品名");
DataColumn dc1 = new DataColumn("尺码");
DataColumn dc2 = new DataColumn("颜色");
DataColumn dc3 = new DataColumn("数量");
DataColumn dc4 = new DataColumn("预售价");
DataColumn dc5 = new DataColumn("实售价");
DataColumn dc6 = new DataColumn("折扣率");
DataColumn dc7 = new DataColumn("销售员");
dt.Columns.Add(dc);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
dt.Columns.Add(dc6);
dt.Columns.Add(dc7);
}
DataRow dr = dt.NewRow();
dr["商品名"] = _Name;
dr["尺码"] = _Size;
dr["颜色"] = _Color;
dr["数量"] = _Num;
dr["预售价"] = db.Stock(_Id).Tables[0].Rows[0][7].ToString().Trim();
dr["实售价"] = lblYmoney.Text;
dr["折扣率"] = _Dis;
dr["销售员"] = user._Name;
dt.Rows.Add(dr);
this.dataGridView1.DataSource = dt;
}
#endregion
- BLL层代码
public int AddSell(DataTable dt)
{
int count=0;//计算数据库影响行数
for (int i = 0; i < dt.Rows.Count; i++)
{
string sql = string.Format("insert sell values('{0}','{1}','{2}','{3}',{4},'{5}','{6}',{7},DEFAULT)", dt.Rows[i][7].ToString(), dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString(), dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString(), dt.Rows[i][5].ToString(), dt.Rows[i][6].ToString());
count += ds.AddSell(sql);
}
return count;
}
参数自己对应就可以,这里需要注意一点,不能使用foreach循环,因为DataTable和DataGridView都不包含GetEnumerator的公共定义
- DAL层代码
public int AddSell(string sql)
{
return db.ExecuteNonQueryCount(sql);
}
- DBHelp
#region 链接数据库
public SqlConnection Open()
{
string str = string.Format("Data Source=(local);Initial Catalog=drees;Integrated Security=True");
SqlConnection con = new SqlConnection(str);
if (con.State == ConnectionState.Open)
{
con.Close();
}
else
{
con.Open();
}
return con;
}
#endregion
#region 执行添加 删除 更新 并返回受影响行数
public int ExecuteNonQueryCount(string sql)
{
SqlCommand com = new SqlCommand(sql,Open());
return com.ExecuteNonQuery();
}
#endregion