private void sellbook() { if (string.IsNullOrEmpty(textBox1.Text)) { MessageBox.Show("请输入条码后查询"); textBox1.Focus(); return; } if (0 == string.Compare("YG", textBox1.Text.Substring(0, 2), true)) { //二手书 //先判断是否已经添加了相同的条码 for (int i = 0; i < g_dt_o.Rows.Count; i++) { if (g_dt_o.Rows[i]["id"].ToString() == textBox1.Text) { MessageBox.Show("已经添加了该出售信息,无法多次添加"); return; } } DataTable dt = ygw.GetOldBookInfoById(textBox1.Text); if (dt.Rows.Count != 1) { MessageBox.Show(string.Format("查找二手书交易信息出错!共查询到【{0}】条交易信息", dt.Rows.Count.ToString())); return; } else { dt.Columns.Add("off", System.Type.GetType("System.String")); dt.Columns.Add("totalprice", System.Type.GetType("System.String")); if (g_dt_o.Rows.Count == 0) { g_dt_o = dt.Copy(); g_dt_o.Rows.Clear(); } g_dt_o.ImportRow(dt.Rows[0]); } } else { //先判断是否已经添加了相同的条码 DataTable dt = ygw.SearchBookinfoByIsbn(textBox1.Text); if (dt.Rows.Count > 1) { 图书选择 f = new 图书选择(dt); f.MyEvent += new 图书选择.MyDelegate(HandleChoseResult);//监听b窗体事件 f.Show(); } else if (dt.Rows.Count == 1) { //一个 for (int i = 0; i < g_dt_n.Rows.Count; i++) { if (g_dt_n.Rows[i]["bookid"].ToString() == dt.Rows[0]["id"].ToString()) { MessageBox.Show("已经添加了该出售信息,无法多次添加"); return; } } DataRow dr = g_dt_n.NewRow(); dr["bookid"] = dt.Rows[0]["id"].ToString(); dr["name"] = dt.Rows[0]["name"].ToString(); dr["press"] = dt.Rows[0]["press"].ToString(); dr["price"] = dt.Rows[0]["price"].ToString(); dr["type"] = "新书"; dr["count"] = "1"; dr["off"] = "0.8"; dr["totalprice"] = (Convert.ToDouble(dt.Rows[0]["price"].ToString()) * 0.8).ToString(); g_dt_n.Rows.Add(dr); } else { MessageBox.Show("没有查询到ISBN为【" + textBox1.Text + "】的图书"); } } BindingDataTable(); }
public DataTable SearchBookinfoByIsbn(string isbn) { DataTable dt = ygw.SearchBookinfoByIsbn(isbn); if (dt.Rows.Count > 0) { //如果本地已经有图书信息,就直接返回给用户 return(dt); } //本地没有图书信息,从主数据库取信息 dt = new DataTable(); //初始化dt结构 dt.Columns.Add("id", System.Type.GetType("System.Int32")); dt.Columns.Add("name", System.Type.GetType("System.String")); dt.Columns.Add("press", System.Type.GetType("System.String")); dt.Columns.Add("isbn", System.Type.GetType("System.String")); dt.Columns.Add("price", System.Type.GetType("System.String")); dt.Columns.Add("author", System.Type.GetType("System.String")); try { JObject jo; if (yg.SearchBookinfoByIsbn(isbn, out jo)) { YouGeWinformApi.Localbookinfo lbi = new YouGeWinformApi.Localbookinfo(); lbi.author = jo["data"]["author"].ToString(); lbi.fixedprice = jo["data"]["fixedPrice"].ToString(); lbi.guid = jo["data"]["id"].ToString();; lbi.imgpath = jo["data"]["imgpath"].ToString(); lbi.isbn = jo["data"]["isbn"].ToString(); lbi.name = jo["data"]["name"].ToString(); lbi.press = jo["data"]["press"].ToString(); //如果从主数据库取到了图书信息,那么就写入本地数据库 string newid; if (ygw.InsertNewBookInfo(lbi, out newid)) { //写入成功之后,将dt变量赋值并返回 DataRow dr = dt.NewRow(); dr["id"] = Convert.ToInt32(newid); dr["name"] = lbi.name; dr["press"] = lbi.press; dr["isbn"] = lbi.isbn; dr["price"] = lbi.fixedprice; dr["author"] = lbi.author; dt.Rows.Add(dr); return(dt); } else { return(dt); } } else { return(dt); } } catch { //异常返回不一定是错误,jo对象解析完毕也会走到这里 return(dt); } }