2018-05-23

2.8 商品信息錄入界面功能設計

2.8.1 GIF圖片

2.8.1.gif

2.8.2 主要功能

2.8.2.1 快速登入錄入商品信息
2.8.2.2 快速修改其密碼

2.8.3 數(shù)據(jù)庫表結構

數(shù)據(jù)庫表1.PNG
數(shù)據(jù)庫表2.PNG

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

ADO.NET.PNG

具體步驟:

  1. 導入命名空間;
  2. 定義數(shù)據(jù)庫連接字符串,運用Connection對象建立與數(shù)據(jù)庫連接;
  3. 打開連接;
  4. 利用Command對象的ExecuteNoQuery()方法執(zhí)行Insert語句;
  5. 通過ExecuteNoQuery()方法返回值判斷是否修改成功,并在界面上提示;
  6. 關閉連接

2.8.5 迭代過程

(1)無外鍵


無外鍵.png

(2)有外鍵


有外鍵.PNG

代碼

this.cbb_Supplier.DataSource = ds.Tables["MySupplier"];
this.cbb_Supplier.DisplayMember = "NAME"; // ComboBox下拉列表顯示的內容,這里顯示供應商名稱
this.cbb_Supplier.ValueMember = "CODE";   // ComboBox另外還攜帶一個隱藏的值叫ValueMember,指定為供應商代碼
this.cbb_Supplier.SelectedIndex = 0;

2.8.6ComboBox數(shù)據(jù)綁定流程

ComboBox.PNG

ComboBox數(shù)據(jù)源綁定的三個要素:
1) 設置DataSource屬性
2) 設置DisplayMember屬性
3) 設置ValueManager屬性
代碼

this.comboBox1.DataSource = ds.Tables["MySupplier"];
this.comboBox1.DisplayMember = "NAME";
this.comboBox1.ValueMember = "CODE";

2.8.7 重要代碼片段

(1)向數(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();
// 構造命令
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("商品信息錄入失敗");
}


}
catch (Exception exp)
{
    MessageBox.Show(“訪問數(shù)據(jù)庫錯誤:” + exp.Message);
}
finally
{
    sqlConn.Close();
}

(2)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)來標識數(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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容