c#连接SQL Server 数据库

1.建立一个控制台测试程序简单实现
c#连接SQL Server 数据库
   using (SqlConnection conn = new SqlConnection("Server=.;database=DB_Test;uid=sa;pwd=111111"))
                {
                    //查询
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("select * from test", conn);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);

                    Console.WriteLine("查询出条目数:" + ds.Tables[0].Rows.Count.ToString());


                    

                }
                using (SqlConnection conn = new SqlConnection("Server=.;database=DB_Test;uid=sa;pwd=111111"))
                {

                    //删除
                    conn.Open();
                    SqlCommand cmd1 = new SqlCommand("delete from test where id=14", conn);
                    int rows = cmd1.ExecuteNonQuery();
                    Console.WriteLine("删除条目数:" + rows);
                

                }
                Console.Read();
c#连接SQL Server 数据库

2.连接字符串配置在config文件中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ConnASK" value="Server=.;database=DB_Test;uid=sa;pwd=111111" />
  </appSettings>
</configuration>

读取

c#连接SQL Server 数据库
 //数据库连接字符串(.config来配置).
        public static string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnASK"];    
        static void Main(string[] args)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    //查询
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("select * from test", conn);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);

                    Console.WriteLine("查询出条目数:" + ds.Tables[0].Rows.Count.ToString());
                }
                Console.Read();
            }
            catch (Exception)
            {
                
                throw;
            }
           
        }
c#连接SQL Server 数据库

 

c#连接SQL Server 数据库,布布扣,bubuko.com

c#连接SQL Server 数据库

上一篇:Oracle GoldenGate 12c 新特性 Credential Store and USERIDALIAS


下一篇:Oracle中的日期处理方法