不好意思,我添加上去了,还是没反应,楼上的请帮我看看哪里还要改,谢了.
代码比较乱,换了好几种方法都没有成功,急啊
namespace 客户信息
{
public partial class Form1 : Form
{
SqlDataAdapter sqlDataAdapter1;
//存取数据库的主要类
SqlCommand sqlCommand1;
//SQL语句处理的类
SqlConnection sqlConnection1;
// 表示是否处于插入新记录的状态
private bool bNewRecord = false;
// 获取所有客户的ID
private void GetCustomID()
{
//SqlDataReader sdr;
////sqlConnection1.Open(); // 打开连接
////sdr = sqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);
//cbxID.Items.Clear();
//while (sdr.Read())
//{
// // 把客户ID插入到组合框控件中
// cbxID.Items.Add(sdr.GetValue(0));
//}
//sdr.Close(); // 关闭SqlDataReader对象和数据库连接
//cbxID.SelectedIndex = 0;
}
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
//SqlDataAdapter sqlDataAdapter1;
//SqlCommand sqlCommand1;
//SqlConnection sqlConnection1;
//private bool bNewRecord = false;
//private void GetCustomID()
//{
// sqlDataReader sdr;
// sqlConnection1.Open();
// sdr = sqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);
// cbxID.Items.Clear();
// while (sdr.Read())
// { cdxID.Items.Add(sdr.GetValue(0));
// }
// sdr.Close();
// cbxID.SelectedIndex = 0;
//}
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void label12_Click(object sender, EventArgs e)
{
}
private void customersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.customersBindingSource.EndEdit();
this.customersTableAdapter.Update(this.northwindDataSet.Customers);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“northwindDataSet.Customers”中。您可以根据需要移动或移除它。
this.customersTableAdapter.Fill(this.northwindDataSet.Customers);
}
/// <summary>
/// 根据行号显示该行中的图书信息
/// </summary>
private void DisplayBookInfo()
{
//定义一个整形来记录选中的行号
int currPosition = this.dataGridView1.CurrentCell.RowIndex;
//将指定的值赋给指定的TextBox
this.textBox1.Text = this.dataGridView1[currPosition, 0].ToString();
this.textBox2.Text = this.dataGridView1[currPosition, 1].ToString();
this.textBox3.Text = this.dataGridView1[currPosition, 2].ToString();
this.textBox6.Text = this.dataGridView1[currPosition, 3].ToString();
this.textBox5.Text = Convert.ToDateTime(this.dataGridView1[currPosition, 4]).ToShortDateString();
this.textBox7.Text = this.dataGridView1[currPosition, 5].ToString();
this.textBox8.Text = this.dataGridView1[currPosition, 6].ToString();
this.textBox4.Text = this.dataGridView1[currPosition, 7].ToString();
this.textBox9.Text = this.dataGridView1[currPosition, 8].ToString();
this.textBox10.Text = this.dataGridView1[currPosition, 10].ToString();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void cbxID_SelectedIndexChanged(object sender, EventArgs e)
{
DisplayBookInfo();
}
private void button1_Click(object sender, EventArgs e)
{
if (cbxID.SelectedIndex > 0)
cbxID.SelectedIndex -= 1;
}
private void button2_Click(object sender, EventArgs e)
{
if (cbxID.SelectedIndex < cbxID.Items.Count - 1)
cbxID.SelectedIndex += 1;
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox6.Text = "";
textBox5.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox4.Text = "";
textBox9.Text = "";
textBox10.Text = "";
cbxID.DropDownStyle = ComboBoxStyle.DropDown;
cbxID.Text = "";
bNewRecord = true;
}
private void button4_Click(object sender, EventArgs e)
{
SqlCommand sqlcmd = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID=@ID",
sqlConnection1);
sqlcmd.Parameters.AddWithValue("@ID", cbxID.Text);
try
{
sqlConnection1.Open();
int rowAffected = sqlcmd.ExecuteNonQuery();
if (rowAffected == 1)
cbxID.Items.RemoveAt(cbxID.SelectedIndex);
}
catch (SqlException ex)
{
MessageBox.Show("删除错误:" + ex.Message, "出现错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button6_Click(object sender, EventArgs e)
{
string sqlStatement;
// 根据是否正在添加新记录来建立适当的查询语句
if (bNewRecord == true)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox6.Text = "";
textBox5.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox4.Text = "";
textBox9.Text = "";
textBox10.Text = "";
sqlStatement = "INSERT INTO Customers VALUES(" +
"'" + cbxID.Text + "'," +
"'" + textBox1.Text + "'," +
"'" + textBox2.Text + "'," +
"'" + textBox3.Text + "'," +
"'" + textBox6.Text + "'," +
"'" + textBox5.Text + "'," +
"'" + textBox7.Text + "'," +
"'" + textBox8.Text + "'," +
"'" + textBox4.Text + "'," +
"'" + textBox9.Text + "'," +
"'" + textBox10.Text + "')";
}
else
{
sqlStatement = "UPDATE Customers SET " +
"CompanyName='" + textBox1.Text + "'," +
"ContactName='" + textBox2.Text + "'," +
"ContactTitle='" + textBox3.Text + "'," +
"Address='" + textBox6.Text + "'," +
"City='" + textBox5.Text + "'," +
"Region='" + textBox7.Text + "'," +
"PostalCode='" + textBox8.Text + "'," +
"Country='" + textBox4.Text + "'," +
"Phone='" + textBox9.Text + "'," +
"Fax='" + textBox10.Text + "'" +
"WHERE CustomerID = '" + cbxID.Text + "'";
}
// 创建SQL命令
SqlCommand sqlcmd = new SqlCommand(
sqlStatement,
sqlConnection1);
try
{
sqlConnection1.Open();
int rowAffected = sqlcmd.ExecuteNonQuery();
if (rowAffected == 1)
cbxID.Items.Add(cbxID.Text);
}
catch (SqlException ex)
{
MessageBox.Show("更新错误:" + ex.Message, "出现错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
sqlConnection1.Close();
}
if (bNewRecord == true)
{
cbxID.DropDownStyle = ComboBoxStyle.DropDownList;
bNewRecord = false;
cbxID.SelectedIndex = cbxID.Items.Count - 1;
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
//try
//{
// this.customersTableAdapter.FillBy(this.northwindDataSet.Customers);
//}
//catch (System.Exception ex)
//{
// System.Windows.Forms.MessageBox.Show(ex.Message);
//}
}
}
}