我找不到错误了,谁帮我看看,谢谢了!~
private void Form1_Load(object sender, EventArgs e)
{
objSqlConnection = new SqlConnection("Integrated Security=SSPI;database =my;server=localhost;");
objSqlDataAdapter = new SqlDataAdapter("select * from Books_Details",objSqlConnection);
DataSet ds = new DataSet();
objSqlDataAdapter.Fill(ds, "Books_Details");
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "Books_Details";
string insCmd = "insert into Books_Details values(@Books_Details,@Name,@Author,@Price,@Qty)";
this.objSqlDataAdapter.InsertCommand = new SqlCommand(insCmd, this.objSqlConnection);
objParam = objSqlDataAdapter.InsertCommand.Parameters.Add(new SqlParameter("@BookAccessCode",System.Data.SqlDbType.VarChar,4));
objParam.SourceColumn = "BookAccessCode";
objParam.SourceVersion = DataRowVersion.Current;
objParam = objSqlDataAdapter.InsertCommand.Parameters.Add(new SqlParameter("@Name",System.Data.SqlDbType.VarChar,10));
objParam.SourceColumn = "Name";
objParam.SourceVersion = DataRowVersion.Current;
objParam = objSqlDataAdapter.InsertCommand.Parameters.Add(new SqlParameter("@Author",System.Data.SqlDbType.VarChar,10));
objParam.SourceColumn = "@Author";
objParam.SourceVersion = DataRowVersion.Current;
objParam = objSqlDataAdapter.InsertCommand.Parameters.Add(new SqlParameter("@Price",System.Data.SqlDbType.Decimal,9));
objParam.SourceVersion = DataRowVersion.Current;
objParam.SourceColumn = "Price";
objParam = objSqlDataAdapter.InsertCommand.Parameters.Add(new SqlParameter("&Qty",System.Data.SqlDbType.Int ,4));
objParam.SourceColumn = "Qty";
objParam.SourceVersion = DataRowVersion.Current;
}
private bool ValidateFields()
{
bool returnValue = true;
if (this.txtBookAccessCode.Text.Trim() == string.Empty)
{
MessageBox.Show("请提供完整的信息");
this.txtBookName.Focus();
return false;
}
else
{
//检查作者名字段有无任何数字数据
for (int i = 0; i < this.txtAuthor.Text.Length; i++)
{
if (char.IsNumber(txtAuthor.Text.ToString(), i))
{
MessageBox.Show("作者名不能是数字");
this.txtAuthor.Focus();
returnValue = false;
break;
}
}
for (int i = 0; i < this.txtPrice.Text.Length; i++)
{
if (!char.IsNumber(txtPrice.Text.ToString(), i))
{
MessageBox.Show("价格应为数字");
this.txtPrice.Focus();
returnValue = false;
break;
}
}
for (int i = 0; i < this.txtQty.Text.Length; i++)
{
if (!char.IsNumber(txtQty.Text.ToString(), i))
{
MessageBox.Show("图书数量应为数字");
this.txtQty.Focus();
returnValue = false;
break;
}
}
}
return returnValue;
}
private void btnInsertBkDt_Click(object sender, EventArgs e)
{
try
{
if (this.txtBookAccessCode.Text != string.Empty && this.txtAuthor.Text != string.Empty
&& this.txtPrice.Text != string.Empty && this.txtQty.Text != string.Empty && this.txtBookName.Text != string.Empty)
{
if (Validate())
{
DataRow objDataRow = ds.Tables["Books_Details"].NewRow();
objDataRow["BookAccessCode"] = this.txtBookAccessCode.Text;
objDataRow["Name"] = this.txtBookName.Text;
objDataRow["Author"] = this.txtAuthor.Text;
objDataRow["Price"] = this.txtPrice.Text;
objDataRow["Qty"] = this.txtQty.Text;
ds.Tables["Books_Details"].Rows.Add(objDataRow);
ds.HasChanges(DataRowState.Added);
objSqlDataAdapter.Update(ds, "Books_Details");
MessageBox.Show("图书成功入库");
this.txtBookName.Clear();
this.txtPrice.Clear();
this.txtQty.Clear();
this.txtBookAccessCode.Clear();
this.txtAuthor.Clear();
this.dataGridView1.Update();
this.txtBookName.Focus();
}
}
else if (ds.HasChanges())
{
objSqlDataAdapter.Update(ds, "Books_Details");
MessageBox.Show("已成功添加记录");
}
else
MessageBox.Show("提供完整的信息");
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.objSqlConnection.Close();
this.Close();
}
private void frmAddBooks_closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.objSqlConnection.Close();
}
}
}