1.连接数据库
在Web.config 里面配置 (数据库)
<connectionStrings>
<add name="rootContext" connectionString="Data Source=XT-20160223QBBZ;Initial Catalog=root;Persist Security Info=True;User ID=sa;Password=123456;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
2. 控制器里面的代码
public ActionResult Message()
{
ViewData["name"] = Request.Form["fname"];
ViewData["message"] = Request.Form["lname"];
int count = 0;
string ConnStr =
System.Configuration.ConfigurationManager.ConnectionStrings["rootContext"].ToString();
//创建DataAdapter 对象
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();
ViewData["state"] = "当前的连接状态:" + conn.State.ToString();
string sqlstr = "Select * From [User]";
da.SelectCommand = new SqlCommand(sqlstr, conn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
Response.Write("用户ID 用户名 密码");
Response.Write("<br />");
//遍历行
foreach (DataRow dr in dt.Rows)
{
//遍历列
for (int i = 0; i < dt.Columns.Count; i++)
{
Response.Write(dr[i].ToString());
}
Response.Write("<br />");
}
return View();
}
3.前台界面的代码
里面并没有放什么代码
所有的代码都这些,希望对新手有所帮助!