一荸实、效果圖
1.gif
二匀们、畫面主要功能:點(diǎn)擊錄入商品信息缴淋,進(jìn)入錄入商品信息界面准给,輸入商品條碼泄朴、商品名稱、商品價(jià)格露氮。商品規(guī)格祖灰、供應(yīng)商以及備注等信息,點(diǎn)擊錄入按鈕即可錄入成功畔规。
捕獲.PNG
三局扶、
圖片3.png
具體步驟
1.導(dǎo)入命名空間;
2.定義數(shù)據(jù)庫連接字符串叁扫,運(yùn)用Connection對(duì)象建立與數(shù)據(jù)庫連接三妈;
3.打開連接;
4.利用Command對(duì)象的ExecuteNoQuery()方法執(zhí)行Insert語句莫绣;
5.通過ExecuteNoQuery()方法返回值判斷是否修改成功畴蒲,并在界面上提示;
6.關(guān)閉連接对室。
四模燥、迭代過程
this.cbb_Supplier.DataSource = ds.Tables["MySupplier"];
this.cbb_Supplier.DisplayMember = "NAME"; // ComboBox下拉列表顯示的內(nèi)容,這里顯示供應(yīng)商名稱
this.cbb_Supplier.ValueMember = "CODE"; // ComboBox另外還攜帶一個(gè)隱藏的值叫ValueMember掩宜,指定為供應(yīng)商代碼
this.cbb_Supplier.SelectedIndex = 0;
五蔫骂、ComboBox數(shù)據(jù)綁定流程
{
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 綁定數(shù)據(jù)源
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
String sqlStr = "select * from SUPPLIER order by CODE";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 將該查詢過程綁定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 將DataSet和DataAdapter綁定
DataSet ds = new DataSet();
// 自定義一個(gè)表(MySupplier)來標(biāo)識(shí)數(shù)據(jù)庫的SUPPLIER表
adp.Fill(ds, "MySupplier");
// 指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表
this.comboBox1.DataSource = ds.Tables["MySupplier"];
this.comboBox1.DisplayMember = "NAME";
this.comboBox1.ValueMember = "CODE";
this.comboBox1.SelectedIndex = 0;
六、重要代碼片段
向數(shù)據(jù)庫中插入數(shù)據(jù)
String id = this.tb_Id.Text.Trim();
String name = this.tb_Name.Text.Trim();
float price = float.Parse(this.tb_Price.Text.Trim());
String spec = this.tb_Spec.Text.Trim();
String remark = this.tb_Remark.Text.Trim();
// 連接字符串牺汤,注意與實(shí)際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
String sqlStr = "insert into GOODSINFO(ID, NAME, PRICE, SPEC, REMARK) values(@id, @name, @price, @spec, @remark)";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串參數(shù)賦值
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.Parameters.Add(new SqlParameter("@price", price));
cmd.Parameters.Add(new SqlParameter("@spec", spec));
cmd.Parameters.Add(new SqlParameter("@remark", remark));
// 將命令發(fā)送給數(shù)據(jù)庫
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否插入成功
if (res != 0)
{
MessageBox.Show("商品信息錄入成功");
}
else
{
MessageBox.Show("商品信息錄入失敗");
}
ComboBox數(shù)據(jù)源綁定實(shí)踐
private void Form1_Load(object sender, EventArgs e)
{
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 綁定數(shù)據(jù)源
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
String sqlStr = "select * from SUPPLIER order by CODE";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 將該查詢過程綁定到DataAdapter
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
// 將DataSet和DataAdapter綁定
DataSet ds = new DataSet();
// 自定義一個(gè)表(MySupplier)來標(biāo)識(shí)數(shù)據(jù)庫的SUPPLIER表
adp.Fill(ds, "MySupplier");
// 指定ComboBox的數(shù)據(jù)源為DataSet的MySupplier表
this.comboBox1.DataSource = ds.Tables["MySupplier"];
this.comboBox1.DisplayMember = "NAME";
this.comboBox1.ValueMember = "CODE";
this.comboBox1.SelectedIndex
編寫增強(qiáng)功能的錄入商品信息界面
String id = this.tb_Id.Text.Trim();
String name = this.tb_Name.Text.Trim();
float price = float.Parse(this.tb_Price.Text.Trim());
String spec = this.tb_Spec.Text.Trim();
String remark = this.tb_Remark.Text.Trim();
// 連接字符串辽旋,注意與實(shí)際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["SuperMarketSales"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
}
catch (Exception exp)
{
MessageBox.Show(“訪問數(shù)據(jù)庫錯(cuò)誤:” + exp.Message);
}
finally
{
sqlConn.Close();
}
String sqlStr = "insert into GOODSINFO(ID, NAME, PRICE, SPEC, REMARK) values(@id, @name, @price, @spec, @remark)";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串參數(shù)賦值
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.Parameters.Add(new SqlParameter("@price", price));
cmd.Parameters.Add(new SqlParameter("@spec", spec));
cmd.Parameters.Add(new SqlParameter("@remark", remark));
// 將命令發(fā)送給數(shù)據(jù)庫
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否插入成功
if (res != 0)
{
MessageBox.Show("商品信息錄入成功");
}
else
{
MessageBox.Show("商品信息錄入失敗");
}