2012-06-27 14 views
-6

我在ASP.Net中創建了一個空項目,然後手動添加了它的視圖,控制器和模型。現在我想將SQL Server連接到Model類。我該怎麼做 ?SQL Server和Model之間的手動連接

+0

它會變得更糟嗎? 「幫助,我不知道如何編程,我不願意閱讀一些文檔」;) – TomTom

+0

以凱文:我只是問一個問題,但你只是告訴廢話。請彬彬有禮,只是回答問題。 – temp

+0

不,你希望有人爲你做,而不是自己研究 - 這不是什麼。一個簡單的谷歌會很容易回答你的問題 –

回答

0
try with this code, this code contain select query for just example  

    // Is your string connection to your database and server 
    string connectionString = ""; //connection to your database 

     //Create a connection to your database 
     //using bloc , in order to destruct connection object in the end of treatment 
     using (SqlConnection connection = new SqlConnection(connectionString)) 
     { 

      //Create object command who contain your query sql, permit you to get data or  //insert on update or delete data 
      using (SqlCommand command= connection.CreateCommand()) 

       { 
        //for example a query, who select data from table in your databse, 
        command.CommandText = "SELECT * FROM Table where col = @ParameterTest"; 
        //for exxample a parameter for your query 
        command.Parameters.Add("@ParameterTest", 123); //your test value 


        //after create connection you must open this 
        command.Connection.Open(); 
        //get data in reader, structure for readin datas 
        var reader = command.ExecuteDataReader(); 


       } 
     } 
+0

tnx,但你能解釋這個代碼更清楚嗎? – temp

+0

好吧我帶你解決方案中的評論 –

+0

有沒有人試圖幫助我?我真的需要它。請.... – temp