C#作業(yè)
鄧別情
2016270411
一半夷、效果圖
2.8..gif
二、畫面主要功能:點擊錄入商品信息迅细,進(jìn)入錄入商品信息界面巫橄,輸入商品條碼、商品名稱茵典、商品價格湘换。商品規(guī)格、供應(yīng)商以及備注等信息统阿,點擊錄入按鈕即可錄入成功彩倚。
11123570-5cd39e5e6b30637e.png
三、
TYFI.png
具體步驟
1.導(dǎo)入命名空間扶平;
2.定義數(shù)據(jù)庫連接字符串帆离,運用Connection對象建立與數(shù)據(jù)庫連接;
3.打開連接结澄;
4.利用Command對象的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另外還攜帶一個隱藏的值叫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ù)庫錯誤:" + 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();
// 自定義一個表(MySupplier)來標(biāo)識數(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;
六餐曼、重要代碼片段
ComboBox數(shù)據(jù)源綁定實踐
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ù)庫錯誤:" + 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();
// 自定義一個表(MySupplier)來標(biāo)識數(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
編寫增強功能的錄入商品信息界面
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();
// 連接字符串压储,注意與實際環(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ù)庫錯誤:” + 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("商品信息錄入失敗");
}
向數(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();
// 連接字符串鲜漩,注意與實際環(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ù)庫錯誤:" + 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("商品信息錄入失敗");
}