ADO. NET 에서 SQL Command 의 실행

8594 단어 command
프로젝트 에 프로필 추가:
 
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <connectionStrings>

    <add name ="Students"

         providerName ="System.Data.SqlClient"

         connectionString ="server=(local); integrated security = SSPI;

         database = Students"/>

  </connectionStrings>

</configuration>


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
다음 코드 에는 설정 파일 의 연결 문자열 을 참조 하 는 방법 과 SQL Command 가 실행 하 는 네 가지 방법 이 포함 되 어 있 습 니 다.
메모: system. configuration system. Transaction 은 수 동 으로 Reference 에 가입 해 야 합 니 다.
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

using System.Data.Common;

using System.Configuration;

using System.Data.SqlClient;

using System.Transactions;

using System.Xml;



namespace SQLConnDemo

{

    class Program

    {

        private DbConnection GetDatabaseConnection(string name)

        {

            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[name];



            DbProviderFactory factory = DbProviderFactories.GetFactory(settings.ProviderName);



            DbConnection conn = factory.CreateConnection();

            conn.ConnectionString = settings.ConnectionString;



            return conn;

        }



        private static string GetConnectionStringsConfig(string connectionName)

        {

            string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString();

            Console.WriteLine(connectionString);

            return connectionString;

        }



        static void Main(string[] args)

        {

            string source = GetConnectionStringsConfig("Students");



            try

            {

                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))

                {

                    using (SqlConnection conn = new SqlConnection(source))

                    {

                        // Open the connenction

                        conn.Open();



                        // ExecuteNonQuery Method Applies to Update, Insert, Delete SQL statement

                        string strUpdate = "UPDATE XS " + "SET    = '        ,     ' " + "Where    = '   '";

                        SqlCommand cmd = new SqlCommand(strUpdate, conn);

                        int rowReturned = cmd.ExecuteNonQuery();

                        Console.WriteLine("{0} rows returned", rowReturned);



                        // ExecuteReader Method

                        string strSelect = "SELECT   ,   FROM XS";

                        SqlCommand cmd2 = new SqlCommand(strSelect, conn);

                        SqlDataReader reader = cmd2.ExecuteReader();

                        while (reader.Read())

                        {

                            Console.WriteLine("Student ID: {0}", reader[0]);

                        }

                        reader.Close();

                        

                        // ExecuteScalar Method

                        string countselect = "SELECT COUNT(*) FROM XS";

                        SqlCommand cmd3 = new SqlCommand(countselect, conn);

                        Object o = cmd3.ExecuteScalar();

                        Console.WriteLine(o);                        

 

                        // ExecuteXmlReader Method

                        string strXmlSelect = "SELECT   ,   FROM XS" + " FOR XML AUTO";

                        SqlCommand cmd4 = new SqlCommand(strXmlSelect, conn);

                        XmlReader xr = cmd4.ExecuteXmlReader();

                        xr.Read();

                        string data;

                        do

                        {

                            data = xr.ReadOuterXml();

                            if (!string.IsNullOrEmpty(data))

                                Console.WriteLine(data);

                        } while (!string.IsNullOrEmpty(data));

                        xr.Close();                      





                        // Do something useful



                        //Mark complete

                        scope.Complete();





                        // Close the connection

                        conn.Close();

                    } 

                }                

            }

            catch(SqlException ex)

            {

                // Log the exception

                Console.WriteLine(ex.Message);

            }

        }

    }

}


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

좋은 웹페이지 즐겨찾기