标题:谁知道哪错了????
取消只看楼主
笑笑
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-7-8
 问题点数:0 回复次数:0 
谁知道哪错了????

我找不到错误了,谁帮我看看,谢谢了!~

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();
}
}
}

搜索更多相关主题的帖子: localhost database private server 
2006-07-16 09:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-78566-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.463777 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved