C#如何先将数据库中的数据读出来,然后进行修改???
SqlConnectionconn;privatevoidFormDepartmentFilesModInfo_Load(objectsender,EventArgse)...
SqlConnection conn;
private void FormDepartmentFilesModInfo_Load(object sender, EventArgs e)//窗体
{
conn = new SqlConnection("server=172.16.43.55;database=CateringDB;uid=sa;pwd=123456");//连接数据库
}
private void button1_Click(object sender, EventArgs e)//确定按钮
{
if (Textname.Text == "")
{
MessageBox.Show("部门名称不能为空,请输入部门名称");
}
else
{
conn.Open();//打开数据库连接
SqlCommand cmd = new SqlCommand("select * from tbl_Department_Info where deptName='" + Textname.Text.Trim() + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
//将在数据库中查询到的信息显示出来
}
else
{
MessageBox.Show("该部门不存在");
}
sdr.Close();
conn.Close();
}
}
private void button3_Click(object sender, EventArgs e)//修改按钮
{
}
我这用的都是文本框,不知道对不对,求大神指点下!!! 展开
private void FormDepartmentFilesModInfo_Load(object sender, EventArgs e)//窗体
{
conn = new SqlConnection("server=172.16.43.55;database=CateringDB;uid=sa;pwd=123456");//连接数据库
}
private void button1_Click(object sender, EventArgs e)//确定按钮
{
if (Textname.Text == "")
{
MessageBox.Show("部门名称不能为空,请输入部门名称");
}
else
{
conn.Open();//打开数据库连接
SqlCommand cmd = new SqlCommand("select * from tbl_Department_Info where deptName='" + Textname.Text.Trim() + "'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
//将在数据库中查询到的信息显示出来
}
else
{
MessageBox.Show("该部门不存在");
}
sdr.Close();
conn.Close();
}
}
private void button3_Click(object sender, EventArgs e)//修改按钮
{
}
我这用的都是文本框,不知道对不对,求大神指点下!!! 展开
展开全部
你用dataset获取数据先,然后转换成你的对象,再给你的窗体赋值
更多追问追答
追问
能不能稍微用代码举例下?
追答
//这里是遍历ds的数据
int rowsCount = ds.Tables[0].Rows.Count;
if (rowsCount > 0)
{
for (int i = 0; i < rowsCount; i++)
{
Model.DecoderChannel oDecoderChannel = new Model.DecoderChannel();
if (ds.Tables[0].Rows[i]["channel_id"] != null && ds.Tables[0].Rows[i]["channel_id"].ToString() != "")
{
oDecoderChannel.DecoderChannelID = int.Parse(ds.Tables[0].Rows[i]["channel_id"].ToString());
}
lstDecoderChannel.Add(oDecoderChannel);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询