-6

我在 ASP.Net 中创建了一个空项目,然后手动添加它视图、控制器和模型。现在我想将 SQL Server 连接到 Model 类。我该怎么做 ?

4

1 回答 1

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();


                }
        }
于 2012-06-27T11:38:50.933 回答