注册 登录
编程论坛 C# 论坛

如何将存储过程查询的结果放在dataGridView中显示出来

hxhfg 发布于 2007-08-13 00:44, 3119 次点击

如何将存储过程满足条件的查询结果放在dataGridView中显示出来
try
{
string directory = Directory.GetCurrentDirectory() + "\\aa.ini";

IniFile db = new IniFile(directory);
SqlConnection conn = db.GetDBConn();
conn.Open();

SqlCommand cm = new SqlCommand("kefang_fanghaocha2", conn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add("@fang",SqlDbType.VarChar);
cm.Parameters["@fang"].Value = textBox5.Text;
SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
DataSet ds = new DataSet();
dr.Fill(ds, "fanghao");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "fanghao";

}
else
{
MessageBox.Show("找不到该记录!!");
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

[此贴子已经被作者于2007-8-13 2:51:17编辑过]

8 回复
#2
hxhfg2007-08-13 00:44
查询如何调用存储过程?
#3
卡卡艾2007-08-13 08:26
SqlCommand cm = new SqlCommand("kefang_fanghaocha2", conn);
cm.CommandType = CommandType.StoredProcedure;
这就是调用存储过呈.
#4
hxhfg2007-08-13 09:08
如果将查询出来的结果显示在dataGridView中?
#5
师妃暄2007-08-13 10:25
dataGridView.Sorce=ds.table[0];
#6
hxhfg2007-08-13 10:55
以下是引用师妃暄在2007-8-13 10:25:50的发言:
dataGridView.Sorce=ds.table[0];

我红色部份如何写??这里如何写,帮我一下
DataSet ds = new DataSet();
dr.Fill(ds, "fanghao");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "fanghao";

#7
hxhfg2007-08-13 10:57

我用最笨的方法,如下,行了,可是好笨的,有没有写简单点的
try
{
string directory = Directory.GetCurrentDirectory() + "\\aa.ini";

IniFile db = new IniFile(directory);
SqlConnection conn = db.GetDBConn();
conn.Open();

SqlCommand cm = new SqlCommand("kefang_fanghaocha2", conn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add("@fang", SqlDbType.VarChar);
cm.Parameters["@fang"].Value = textBox5.Text;
SqlDataReader dr = cm.ExecuteReader();
if (dr.Read())
{
conn.Close();
string directory1 = Directory.GetCurrentDirectory() + "\\aa.ini";
IniFile db1 = new IniFile(directory);
SqlConnection conn1 = db1.GetDBConn();
conn1.Open();

SqlDataAdapter myCommand = new SqlDataAdapter("kefang_fanghaocha2", conn);
myCommand.SelectCommand.CommandType = CommandType.StoredProcedure;

myCommand.SelectCommand.Parameters.Add("@fang", SqlDbType.VarChar);
myCommand.SelectCommand.Parameters["@fang"].Value = textBox5.Text;

DataSet ds = new DataSet();
myCommand.Fill(ds, "fanghao");
DataTable dt = ds.Tables["fanghao"];
dataGridView1.DataSource = dt.DefaultView;
}
else
{
MessageBox.Show("找不到该记录!!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

#8
师妃暄2007-08-13 12:36

using(SqlConnection conn =new SqlConnection(connstr))
{
SqlCommand cm = new SqlCommand("kefang_fanghaocha2", conn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Add("@fang",SqlDbType.VarChar);
cm.Parameters["@fang"].Value = textBox5.Text;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cm;
DataSet ds = new DataSet();
dr.Fill(ds);
}
dataGridView1.DataSource = ds;

#9
hqw0826302552014-03-08 14:59
你返回的是一个结果集~
需要逐条加到dataset中去~
1