求~~~通过SQLDataReader读取数据的过程
有么有人知道啊要求写出类名函数名大致框架就好了~~~..大家帮帮我吧~~~~~俺给一个老师的程序...是参考程序publicstaticvoidExecuteSql(st...
有么有人知道啊
要求 写出类名 函数名 大致框架就好了~~~
..大家帮帮我吧~~~~~
俺给一个老师的程序...是参考程序
public static void ExecuteSql(string source)
{
string select = "SELECT ContactName,CompanyName FROM Customers";
try
{
using (SqlConnection conn = new SqlConnection(source))
{
conn.Open();
SqlCommand cmd = new SqlCommand(select, conn);
using (SqlDataReader reader = cmd.ExecuteReader())
{
Console.WriteLine("*** SqlProvider ***");
Console.WriteLine("Output from direct SQL statement...");
Console.WriteLine();
Console.WriteLine("CONTACT COMPANY");
Console.WriteLine("--------------------------------------");
while (reader.Read())
{
Console.WriteLine("{0,-30} {1}", reader[0], reader[1]);
}
reader.Close();
}
conn.Close();
}
}
catch (Exception e) { Console.WriteLine(e.ToString()); }
} 展开
要求 写出类名 函数名 大致框架就好了~~~
..大家帮帮我吧~~~~~
俺给一个老师的程序...是参考程序
public static void ExecuteSql(string source)
{
string select = "SELECT ContactName,CompanyName FROM Customers";
try
{
using (SqlConnection conn = new SqlConnection(source))
{
conn.Open();
SqlCommand cmd = new SqlCommand(select, conn);
using (SqlDataReader reader = cmd.ExecuteReader())
{
Console.WriteLine("*** SqlProvider ***");
Console.WriteLine("Output from direct SQL statement...");
Console.WriteLine();
Console.WriteLine("CONTACT COMPANY");
Console.WriteLine("--------------------------------------");
while (reader.Read())
{
Console.WriteLine("{0,-30} {1}", reader[0], reader[1]);
}
reader.Close();
}
conn.Close();
}
}
catch (Exception e) { Console.WriteLine(e.ToString()); }
} 展开
2个回答
展开全部
//...前面省略
using System.Data.SqlClient; //1:引入命名空间
namespace Test
{
public partial class Form1 : Form
{
static string conStr = "server=.; database = myqq; uid=sa; pwd=0000;"; //2: 定义连接字符串
SqlConnection con = new SqlConnection(conStr); //3: 定义数据库连接对象
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sql = "select * from Users"; //要查询的sql语句
SqlCommand cmd = new SqlCommand(sql, con); //4:创建sqlcommand对象, 执行sql语句
con.Open(); //5: 打开数据库连接
SqlDataReader dr = cmd.ExecuteReader(); //6: 执行sql语句并返回 sqldatareader对象 <相当赋值>
while (dr.Read()) //7:利用sqldatareader读取数据
{
MessageBox.Show("姓名是:"+dr["Uname"].ToString()); //只是举了一个例子
}
dr.Close(); //8:关闭sqldatareader
con.Close(); //9:关闭数据库连接
// 利用sqldatareader读取数据的时候,数据连接必须处于打开状态; 读取方式是 一行一行向前读取
}
}
}
using System.Data.SqlClient; //1:引入命名空间
namespace Test
{
public partial class Form1 : Form
{
static string conStr = "server=.; database = myqq; uid=sa; pwd=0000;"; //2: 定义连接字符串
SqlConnection con = new SqlConnection(conStr); //3: 定义数据库连接对象
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sql = "select * from Users"; //要查询的sql语句
SqlCommand cmd = new SqlCommand(sql, con); //4:创建sqlcommand对象, 执行sql语句
con.Open(); //5: 打开数据库连接
SqlDataReader dr = cmd.ExecuteReader(); //6: 执行sql语句并返回 sqldatareader对象 <相当赋值>
while (dr.Read()) //7:利用sqldatareader读取数据
{
MessageBox.Show("姓名是:"+dr["Uname"].ToString()); //只是举了一个例子
}
dr.Close(); //8:关闭sqldatareader
con.Close(); //9:关闭数据库连接
// 利用sqldatareader读取数据的时候,数据连接必须处于打开状态; 读取方式是 一行一行向前读取
}
}
}
展开全部
public void testReader(){
SqlConnection conn=new SqlConnection();
conn.ConnectionString="server=127.0.0.1;
initial catalog=Stu_xxx;user id=sa;password=sa ";
SqlCommand cmd=new SqlCommand ();
cmd.Connection=conn;
cmd.CommandText="select * from students where sname like '张%'";
conn.Open();
SqlDataReader sdr=cmd.ExecuteReader();
while(sdr.Read())
msg.Text+=“学号:”+sdr[“sid”].ToString()+
“姓名:”+sdr[“sname”].ToString()+
“生日:” +sdr[“sbirthday”].ToString()+”<br>”;
conn.Close();
}
SqlConnection conn=new SqlConnection();
conn.ConnectionString="server=127.0.0.1;
initial catalog=Stu_xxx;user id=sa;password=sa ";
SqlCommand cmd=new SqlCommand ();
cmd.Connection=conn;
cmd.CommandText="select * from students where sname like '张%'";
conn.Open();
SqlDataReader sdr=cmd.ExecuteReader();
while(sdr.Read())
msg.Text+=“学号:”+sdr[“sid”].ToString()+
“姓名:”+sdr[“sname”].ToString()+
“生日:” +sdr[“sbirthday”].ToString()+”<br>”;
conn.Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询