我在ASP.Net中创建了一个空项目,然后手动添加了视图,控制器和模型。现在我想将SQL Server连接到Model类。我该怎么做 ?
答案 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();
}
}