标题:求助:关于数据库的应用的问题
只看楼主
aubblove
Rank: 1
等 级:新手上路
帖 子:117
专家分:0
注 册:2007-8-18
 问题点数:0 回复次数:6 
求助:关于数据库的应用的问题

谁能给出源代码:效果如图
说明用户可以使用窗体中的"客户ID"下拉列表选择在右边表中的一个客户的ID来查看他的信息或者通过上一条和下一条来逐条浏览记录.可以使用添加,删除来更改右边表中的内容.右边的表是从Northwind数据库的表Customers绑定的.

我主要是不知道怎么才能每条记录放到左边的各个textBox里.我是新手,请指教!!谢了!
搜索更多相关主题的帖子: 数据库 源代码 客户 ID 应用 
2007-09-19 15:09
jxnuwy04
Rank: 2
等 级:新手上路
威 望:4
帖 子:768
专家分:0
注 册:2006-9-15
得分:0 
我这里有一段源码给你看看吧:
    //选中某行发生的事件
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
DisplayBookInfo();
}
/// <summary>
/// 根据行号显示该行中的图书信息
/// </summary>
private void DisplayBookInfo()
{
//定义一个整形来记录选中的行号
int currPosition=this.dataGrid1.CurrentCell.RowNumber;

//将指定的值赋给指定的TextBox
this.txtBookIDD.Text=this.dataGrid1[currPosition,0].ToString();
this.txtISBND.Text=this.dataGrid1[currPosition,1].ToString();
this.txtBookNameD.Text=this.dataGrid1[currPosition,2].ToString();
this.txtAuthorD.Text=this.dataGrid1[currPosition,3].ToString();
this.txtPublishDateD.Text=Convert.ToDateTime(this.dataGrid1[currPosition,4]).ToShortDateString();
this.txtBookVersionD.Text=this.dataGrid1[currPosition,5].ToString();
this.txtWordCountD.Text=this.dataGrid1[currPosition,6].ToString();
this.txtPageCountD.Text=this.dataGrid1[currPosition,7].ToString();
this.txtPublisherD.Text=this.dataGrid1[currPosition,8].ToString();
this.cbBookTyprB1.SelectedValue=this.dataGrid1[currPosition,10].ToString();
}
对你应该有所帮助吧.

------------------不为别的,就为你,我的理想!-----------------
2007-09-19 15:14
卡卡艾
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:672
专家分:0
注 册:2007-4-3
得分:0 
楼上的正解.

革命尚未成功,同志仍需努力-----+++
2007-09-19 16:27
aubblove
Rank: 1
等 级:新手上路
帖 子:117
专家分:0
注 册:2007-8-18
得分:0 

不好意思,我添加上去了,还是没反应,楼上的请帮我看看哪里还要改,谢了.
代码比较乱,换了好几种方法都没有成功,急啊
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);
//}
}
}
}

2007-09-20 16:33
卡卡艾
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:672
专家分:0
注 册:2007-4-3
得分:0 
没有反应?

革命尚未成功,同志仍需努力-----+++
2007-09-20 19:13
卡卡艾
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:672
专家分:0
注 册:2007-4-3
得分:0 
this.textBox5.Text = Convert.ToDateTime(this.dataGridView1[currPosition, 4]).ToShortDateString();
改成
this.textBox5.Text = this.dataGridView1[currPosition, 4]).ToString();
我只看了你红色部分的代码.不知道其他地方有没有问题.


革命尚未成功,同志仍需努力-----+++
2007-09-20 19:17
jxnuwy04
Rank: 2
等 级:新手上路
威 望:4
帖 子:768
专家分:0
注 册:2006-9-15
得分:0 

真的不好意思,下午的时候刚好那个时候有点事走开了,晚上回到寝室网速又慢的出奇,更本就进不了这个论坛,直到现在好点了,才看这个,真的是不好意思!
不知道楼主提示的是什么错误?代码太长了,可能写代码的风格不同看起来有点累.


------------------不为别的,就为你,我的理想!-----------------
2007-09-20 23:00



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




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

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