使用C#.net从sql中恢复数据 [英] retriving data from sql using C#.net
本文介绍了使用C#.net从sql中恢复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在C#.net ....中从SQL serever 2005逐行检索数据
how to retrive data row by row in C#.net....from SQL serever 2005
推荐答案
您可以使用sqldatareader读取数据
下面是示例代码
使用System;
使用System.Data.SqlClient;
class program
{
static void Main()
{
/ /
//你需要在这里访问项目的连接字符串。
//
string connectionString = ConsoleApplication1.Properties.Settings.Default。 ConnectionString;
//
//创建新的SqlConnection对象。
//
使用(SqlConnection连接= new SqlConnection(connectionString))
{
connection.Open();
//
//创建新的SqlCommand对象。
//
使用(SqlCommand命令= new SqlCommand(" SELECT * FROM D) ogs1",connection))
{
//
//调用ExecuteReader方法。
//
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
int weight = reader.GetInt32(0); //权重int
string name = reader.GetString(1); //名称字符串
string breed = reader.GetString(2); //品种字符串
//
//将从数据库中读取的值写入屏幕。
//
Console.WriteLine(" Weight = {0},Name = {1},Breed = {2}",weight,name,breed);
}
}
}
}
}
you can read data using sqldatareader
below is the sample code
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
//
// You need to access the project's connection string here.
//
string connectionString = ConsoleApplication1.Properties.Settings.Default.ConnectionString;
//
// Create new SqlConnection object.
//
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
//
// Create new SqlCommand object.
//
using (SqlCommand command = new SqlCommand("SELECT * FROM Dogs1", connection))
{
//
// Invoke ExecuteReader method.
//
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int weight = reader.GetInt32(0); // Weight int
string name = reader.GetString(1); // Name string
string breed = reader.GetString(2); // Breed string
//
// Write the values read from the database to the screen.
//
Console.WriteLine("Weight = {0}, Name = {1}, Breed = {2}", weight, name, breed);
}
}
}
}
}
这篇关于使用C#.net从sql中恢复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文