《2.8 商品信息錄入界面功能設(shè)計(jì)》項(xiàng)目總結(jié)匯報(bào)

  • 總結(jié)人:劉玉紅
    班 級:物應(yīng)1604
    學(xué) 號:2016270314

一、效果圖

效果圖.gif

二、畫面主要功能

  • 用戶可通過點(diǎn)擊錄入商品信息,進(jìn)入錄入商品信息界面,再通過輸入商品條碼、商品名稱、商品價(jià)格、商品規(guī)格、供應(yīng)商以及備注等信息,點(diǎn)擊錄入按鈕即可實(shí)現(xiàn)商品錄入功能。

支持上述功能的后臺數(shù)據(jù)庫表結(jié)構(gòu)如下圖所示:

后臺數(shù)據(jù)庫表.png


三、ADO.NET插入數(shù)據(jù)庫的流程

ADO.NET插入數(shù)據(jù)庫的流程圖.png
  • 具體步驟

1.導(dǎo)入命名空間;
2.定義數(shù)據(jù)庫連接字符串,運(yùn)用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另外還攜帶一個(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ù)據(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ù)據(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 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("商品信息錄入失敗");
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容