5절 1ADONet 연결 SQLServer

3093 단어 sqlserver
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
/*   SQLServer
 *      ,                      ,            ,         
 *     mdf          
 * "Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
 * .\SQLEXPRESS"  "    SQLEXPRESS  ,          SQLEXPRESS,     ."Database1.mdf mdf    "
 *
 * ADO.Net   SqlConnection    SQLServer   ,SqlConnection         ,    using      ,              ok
 * 
 *        
 * 1               ,      SQL Server     
 * 
 * 2     ,   2005      ,  ,  VisualStudio 2008 SP1
 * 
 * 3     ,    
 * 
 * 
 * ADO.Net   SqlConnection    SQLServer   ,SqlConnection         ,ADO.Net       
 *     IDisposable  ,    using      ,              ok
 */
namespace _1ADO.Net  SQLServer
{
    class Program
    {
        static void Main(string[] args)
        {
            /*string dataDir = AppDomain.CurrentDomain.BaseDirectory;
            if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\")) 
            {
                dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
                AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
            }*/
            string dataDir = AppDomain.CurrentDomain.BaseDirectory;
            if (dataDir.EndsWith(@"\bin\Debug\")
                || dataDir.EndsWith(@"\bin\Release\"))
            {
                dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
                AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
            }


            //SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS.AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
            //SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS.AttachDBFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
            SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MyProjects\C#net    \   ADO.NET\1ADO.Net  SQLServer\1ADO.Net  SQLServer\Database1.mdf;Integrated Security=True;User Instance=True");

            //                           
            conn.Open();

            using (SqlCommand cmd = conn.CreateCommand()) 
            {
                cmd.CommandText = "Insert into MyTable1(Name) values('xxdxxd')";
                cmd.ExecuteNonQuery();
                Console.WriteLine("    !");
            }

            Console.WriteLine("         !");
            Console.ReadKey();
        }
    }
}

좋은 웹페이지 즐겨찾기