C# sql 데이타베이스 연결 문자열

SqlConnection myConnection = new SqlConnection ("Data Source=localhost;Initial Catalog=AutoLot;Integrated Security=True;Pooling=False
");

SqlCommand myCommand = new SqlCommand("INSERT INTO [hello] ([hello]) VALUES (@hello)", myConnection);

SqlParameter helloParam = myCommand.Parameters.Add("@hello", SqlDbType.VarChar );

helloParam.Value = MyTextBox.Text;

myConnection.Open();

myCommand.ExecuteNonQuery();

ReplyQuote

이것때문에 한참 고생하다가 겨우... 찾았다
visual studio 2010 의 서버 익스플로러에서 데이타 커넥션 메뉴에 db를 띄워놓고
클릭하면 오른쪽 아래 속성창에

이런식으로 나온다.저 부분을 커넥션 스트링에다가 그냥 대입하면 된다.
문자열을 어떻게 넣어야될지 몰라서 고생했다면... 이렇게 하는것도 좋을듯... 한참고생했네

/**처음에 썼다가 안되서 이것저것 덕지덕지 붙여가지고 소스가 너무 더러워졌다. 엄청난 노가다 소스를 제작했다가 실패한부분
                ////SQL입력,.. 입력할게 너무 많아서 이걸로 못해먹겠다 해결방법은 스트링에 반복문 합체하기로 해서 입력하면 될듯하다.
                string cnStr ="Data Source=localhost;Initial Catalog=AutoLot;Integrated Security=True;Pooling=False";
                string connectionString =//"Source=localhost;Initial Catalog=StockPrice;Persist Security Info=True;User ID=아이디;Password=비번";
                    "Data Source=MSSQLSERVER; Integrated Security=SSPI;Initial Catalog=StockPrice;connection timeout=5";
                    /** "user id=아이디;" +
                                       "password=비밀번호;
server=localhost;" +
                                       "Trusted_Connection=yes;" +
                                       "database=StockPrice; " +
                                       "connection timeout=5";**/
                // Create a connection string via the builder object.
                /**
                SqlConnectionStringBuilder cnStrBuilder = new SqlConnectionStringBuilder();
                cnStrBuilder.InitialCatalog = "StockPrice";
                cnStrBuilder.DataSource = "localhost";
                cnStrBuilder.ConnectTimeout = 10;
                cnStrBuilder.IntegratedSecurity = true;
                **/
                string connectionString = "Data Source=localhost;Initial Catalog=AutoLot;Integrated Security=True;Pooling=False";
                SqlConnection sqlCn = new SqlConnection();

                sqlCn = new SqlConnection();
                sqlCn.ConnectionString = connectionString;
                //sqlCn.ConnectionString = cnStrBuilder.ConnectionString;
                sqlCn.Open();

                // Format and execute SQL statement.
                string sql = string.Format("Insert Into FutureHoga" +
                  "(date, hotime, sametime, futcode, offerho1, offerho2, offerho3, offerho4, offerho5, bidho1, bidho2, bidho3, bidho4, bidho5, offerrem1, offerrem2, offerrem3, offerrem4, offerrem5, bidrem1, bidrem2, bidrem3, bidrem4, bidrem5, offercnt1,offercnt2,offercnt3,offercnt4,offercnt5, bidcnt1,bidcnt2,bidcnt3,bidcnt4,bidcnt5,totofferrem,totofferrem,totoffercnt,totobidcnt" +
                  "('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}', '{11}', '{12}', '{13}', '{14}', '{15}', '{16}', '{17}', '{18}', '{19}', '{20}', '{21}', '{22}', '{23}', '{24}', '{25}', '{26}', '{27}', '{28}', '{29}', '{30}', '{31}', '{32}', '{33}', '{34}', '{35}', '{36}', '{37}', '{38}')",
                  DateTime.Now.Date,
                  m_RealFH0[i].GetFieldData("OutBlock", "hotime"),
                                     (sametime++),
                                     m_RealFH0[i].GetFieldData("OutBlock", "futcode"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offerho1"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offerho2"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offerho3"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offerho4"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offerho5"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidho1"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidho2"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidho3"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidho4"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidho5"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offerrem1"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offerrem2"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offerrem3"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offerrem4"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offerrem5"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidrem1"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidrem2"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidrem3"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidrem4"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidrem5"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offercnt1"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offercnt2"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offercnt3"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offercnt4"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "offercnt5"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidcnt1"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidcnt2"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidcnt3"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidcnt4"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "bidcnt5"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "totofferrem"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "totofferrem"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "totoffercnt"),
                                     m_RealFH0[i].GetFieldData("OutBlock", "totobidcnt"));

                // Execute using our connection.
                using (SqlCommand cmd = new SqlCommand(sql, this.sqlCn))
                {
                    cmd.ExecuteNonQuery();
                }
                sqlCn.Close();//SQL닫기

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available

74 − = 70