zoukankan      html  css  js  c++  java
  • c#取数据库数据 ---两种方法

    通常有以下两种方式 SqlDataReader 和SqlDataAdapter|DataSet方式
    SqlDataReader 方式使用方式如下:
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Data.SqlClient;  
      
    namespace Login  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                //新建一个数据库连接  
                using(SqlConnection conn = new SqlConnection(GetConnectString()))  
                {  
                    conn.Open();//打开数据库  
                    //Console.WriteLine("数据库打开成功!");  
                    //创建数据库命令  
                    SqlCommand cmd = conn.CreateCommand();  
                    //创建查询语句  
                    cmd.CommandText = "SELECT * FROM userinfo";  
                    //从数据库中读取数据流存入reader中  
                    SqlDataReader reader = cmd.ExecuteReader();                 
                      
                    //从reader中读取下一行数据,如果没有数据,reader.Read()返回flase  
                    while (reader.Read())  
                    {  
                        //reader.GetOrdinal("id")是得到ID所在列的index,  
                        //reader.GetInt32(int n)这是将第n列的数据以Int32的格式返回  
                        //reader.GetString(int n)这是将第n列的数据以string 格式返回  
                        int id = reader.GetInt32(reader.GetOrdinal("id"));  
                        string name = reader.GetString(reader.GetOrdinal("name"));  
                        string pwd = reader.GetString(reader.GetOrdinal("password"));  
                        int age = reader.GetInt32(reader.GetOrdinal("age"));  
                        string sex = reader.GetString(reader.GetOrdinal("sex"));  
                        string phone = reader.GetString(reader.GetOrdinal("phone"));  
                        string address = reader.GetString(reader.GetOrdinal("Address"));  
      
                        //格式输出数据  
                        Console.Write("ID:{0},Name:{1},PWD:{2},Age:{3},Sex:{4},Phone{5},Address:{6}
    ", id, name, pwd, age, sex, phone, address);  
                    }  
                }  
                Console.ReadKey();  
            }  
            //得到一个数据库连接字符串  
            static string GetConnectString()  
            {  
                return "Data Source=(local);Initial Catalog=db1;Integrated Security=SSPI;";  
            }  
        }  
    }  
    SqlDataAdapter|DataSet 方式如下
    加入为SQLSERVER数据库:
    using System.Data.SqlClient;
    using System.Data;
     SqlConnection conn = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand("secelt * from userinfo", conn);
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds, "table");
                return ds.Tables["table"];//返回的是一个DataTable
  • 相关阅读:
    GNU C与ANSI C的不同
    在 Windows 上使用 Cygwin
    VS2010程序调试
    VS2010中visual assist x的一些问题
    专访TK教主于旸:原来那些搞安全的说的都是真的(图灵访谈)
    程序员必看的书
    msp430学习笔记-时钟及延时函数
    指针函数与函数指针
    msp430学习笔记-IO及低功耗
    运算符优先级常识
  • 原文地址:https://www.cnblogs.com/liuqifeng/p/9151303.html
Copyright © 2011-2022 走看看