string connstring = "server=localhost;database=maomao;integrated security=SSPI";
SqlConnection myconn = new SqlConnection(connstring );
SqlCommand mycomm = myconn.CreateCommand();
mycomm.CommandText = "INSERT INTO 进货表单 (进货编号,商品名称,进货日期,进货数量,商品单价,进货支出"+
")VALUES(+'"+b+"','"+n+"','"+d+"','"+num+"','"+p+"','"+m+"'"+
")";
myconn.Open();
mycomm.ExecuteNonQuery();
myconn.Close();
store();
}
public void store()
{
string connstring = "server=localhost;database=maomao;integrated security=SSPI";
SqlConnection myconn = new SqlConnection(connstring);
SqlCommand mycomm = myconn.CreateCommand();
mycomm.CommandText = "SELECT 商品名称 FROM 库存表单";
myconn.Open();
SqlDataReader dr = mycomm.ExecuteReader();
while (dr.Read())
{
if (name.Text == dr["商品名称"].ToString())
{
myconn.Close();
cunzai();
}
else
{
myconn.Close();
bucunzai();
}
}
}
public void cunzai()
{
string connstring = "server=localhost;database=maomao;integrated security=SSPI";
SqlConnection myconn = new SqlConnection(connstring);
SqlCommand mycomm = myconn.CreateCommand();
mycomm.CommandText = "UPDATA 库存表单 SET 库存数量+='"+number.Text .Trim()+"'"+
"WHERE 商品名称='"+name.Text.Trim()+"'";
myconn.Open();
mycomm.ExecuteNonQuery();
myconn.Close();
}
public void bucunzai()
{
string connstring = "server=localhost;database=maomao;integrated security=SSPI";
SqlConnection myconn = new SqlConnection(connstring);
SqlCommand mycomm = myconn.CreateCommand();
mycomm.CommandText = "INSERT INTO 库存表单(商品名称,库存数量"+
")VALUES('"+name.Text.Trim()+"','"+number.Text.Trim()+"')";
myconn.Open();
mycomm.ExecuteNonQuery();
myconn.Close();
}
可以么?
string connstring = "server=localhost;database=maomao;integrated security=SSPI";
SqlConnection myconn = new SqlConnection(connstring );
SqlCommand mycomm = myconn.CreateCommand();
mycomm.CommandText = "INSERT INTO 进货表单 (进货编号,商品名称,进货日期,进货数量,商品单价,进货支出"+
")VALUES(+'"+b+"','"+n+"','"+d+"','"+num+"','"+p+"','"+m+"'"+
")";
myconn.Open();
mycomm.ExecuteNonQuery();
myconn.Close();
store();
}
public void store()
{
string connstring = "server=localhost;database=maomao;integrated security=SSPI";
SqlConnection myconn = new SqlConnection(connstring);
SqlCommand mycomm = myconn.CreateCommand();
mycomm.CommandText = "SELECT 商品名称 FROM 库存表单";
myconn.Open();
SqlDataReader dr = mycomm.ExecuteReader();
while (dr.Read())
{
if (name.Text == dr["商品名称"].ToString())
{
cunzai();
}
else
{
bucunzai();
}
}
myconn.Close();
}
public void cunzai()
{
string connstring = "server=localhost;database=maomao;integrated security=SSPI";
SqlConnection myconn = new SqlConnection(connstring);
SqlCommand mycomm = myconn.CreateCommand();
mycomm.CommandText = "UPDATA 库存表单 SET 库存数量+="+number.Text .Trim() +
" WHERE 商品名称='"+name.Text.Trim()+"'";
myconn.Open();
mycomm.ExecuteNonQuery();
myconn.Close();
}
public void bucunzai()
{
string connstring = "server=localhost;database=maomao;integrated security=SSPI";
SqlConnection myconn = new SqlConnection(connstring);
SqlCommand mycomm = myconn.CreateCommand();
mycomm.CommandText = "INSERT INTO 库存表单(商品名称,库存数量) VALUES('"+name.Text.Trim()+"','"+number.Text.Trim()+"')";
myconn.Open();
mycomm.ExecuteNonQuery();
myconn.Close();
}
可以么?
string SelectStr = "SELECT 商品名称 FROM 库存表单 where 商品名称=" + name.Text.Trim();
DataSet ds = null;
ds = myDataSet(SelectStr); //myDataSet()是个返回数据集的方法
string DataStr = "";
判斷是否已經存在此商品在入庫信息
if (ds.Tables[0].Rows.Count > 0) //已經存在
{
DataStr = "UPDATA 库存表单 SET 库存数量=库存数量" + int.Parse(number.Text .Trim()) +
" WHERE 商品名称='" + name.Text.Trim() + "'";
}
else //還不存在
{
DataStr = "INSERT INTO 库存表单(商品名称,库存数量) VALUES('" + name.Text.Trim()+"','" + int.Parse(number.Text.Trim()) + "')";
}
try
{
mycon.myExecuteNonQuery(DataStr); //myExecuteNonQuery()是执行数据库语句的方法
MessageBox.Show("恭喜~!入库成功~!");
}
catch(Exception exp)
{
MessageBox.Show("数据出错啦~!");
}
finally
{
name.Text = "";
number.Text = "";
BindData(); //重新绑定数据
}