Csharp和SQL连接

来源:互联网 发布:曹雪松淘宝技术总监 编辑:程序博客网 时间:2024/06/18 01:24

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace file
{
    class Friends
    {
        static void Main(string[] args)
        {
            try
         {        
          DbConnection connection_f = new SqlConnection();
                DbCommand command_f = new SqlCommand();
                connection_f.ConnectionString = "Data Source= GENGRAN\\SQLEXPRESS;Initial Catalog = Friends;Integrated Security=True";
                //User Id = sa;Password = ghwghw";
                connection_f.Open();
                command_f.Connection = connection_f;
                DbDataAdapter adapter_f = new SqlDataAdapter();
                DataSet ds = new DataSet();
                //command_f.CommandText = "insert into friend values('138',134,null,null)";
                //command_f.ExecuteNonQuery();

                command_f.CommandText = "select * from friend";                  
                adapter_f.SelectCommand = command_f;
                adapter_f.Fill(ds);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    Console.WriteLine(dr["Name"]);
             }
                connection_f.Close();
         }
         catch (Exception ex)
         {
          
          Console.WriteLine(ex.ToString());
         }
            Console.Read();
        }

    }
}

原创粉丝点击