[C#]Connect to sql server

2017-5-20 写技术

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void test() {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=127.0.0.1;database=wasion;user=sa;pwd=123456";
            con.Open();

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandType = CommandType.Text;
            com.CommandText = "select * from ws_user";
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read()) {
                Console.WriteLine("name:{0}\t\tpassword:{1}"    , dr["name"], dr["password"]);
            }
            dr.Close();
            con.Close();
        }

        static void Main(string[] args)
        {
            test();
            Console.ReadKey();
        }
    }
}

标签: C#

发表评论:

Powered by anycle 湘ICP备15001973号-1