数据库的访问!!!!!!!

lfq_adam 2008-08-27 07:49:54
请问我在数据库中输入了一定量的数据!
有一个 A 表 中有 1 , 2 ,3 等数据!
我要如何才能够访问到其中A 表 的 2 数据!
显示在我的dataGridView1 控键中呢?
是用C# 编写的!



跪求!!!!!!!!!!!
...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
changjiangzhibin 2008-08-27
  • 打赏
  • 举报
回复

using System.Data;
using System.Data.SqlClient;

//读库,绑定

//col1 col2 col3
//1 2 3
//则可用 sql = "select col2 from A";

//col1 col2 col3
// 1 aa aaaa
// 2 bb bbbb
// 3 cc cccc
//则可用 sql = "select * from A where col1=2";

//假设为第二种情况
string connStr = "server=.;uid=sa;pwd=;database=myDB"; //myDB为数据库名
SqlConnection conn = new SqlConnection(connStr);
string sql = "select * from A where col1=2";
SqlDataAdapter adp = new SqlDataAdapter(sql,conn);
DataTable dt = new DataTable();
adp.Fill(dt);
this.DataGridView1.DataSource = dt.DefaultView;
初吻给了烟 2008-08-27
  • 打赏
  • 举报
回复
private void button2_Click(object sender, EventArgs e)
{
string strSql = "select * from 人员信息 where Id = 2";
dt = link.relateData(strSql);
dataGridView1.DataSource = dt;
cmdOrders = (CurrencyManager)BindingContext[dt];
}
lfq_adam 2008-08-27
  • 打赏
  • 举报
回复
private void button2_Click(object sender, EventArgs e)
{
string strSql = "select * from 人员信息 ";
dt = link.relateData(strSql);
dataGridView1.DataSource = dt;
cmdOrders = (CurrencyManager)BindingContext[dt];
}

internal DataTable relateData(string strSql)
{
using (SqlConnection conn = new SqlConnection(connectString))
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "temptable");
DataTable dt = ds.Tables["temptable"];
return dt;
}
}

我这样写就是把所有的数据都输出 如何才能查找输出呢??


急!!!!!鬼求!!!
初吻给了烟 2008-08-27
  • 打赏
  • 举报
回复

第2行的第1列
string ss = dataGridView1.Rows[1][0].ToString();


第2行的第2列
string ss = dataGridView1.Rows[1][1].ToString();


第2行的第3列
string ss = dataGridView1.Rows[1][2].ToString();


.........................

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧