Code (C#)
สร้าง Class DBConnection.cs ขึ้นมา เก็บไว้ใน foder ชื่อ Class ใน solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace TEST.Class
{
public class DBConnection
{
public static string strConnString = "Data Source=.\\SQLEXPRESS;User ID=sa;Password=sa;Initial Catalog=MYBASE;Integrated Security=True";
}
}
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using TEST.Class;
namespace TEST
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection objConn;
SqlCommand objCmd;
StringBuilder sb;
protected void Page_Load(object sender, EventArgs e)
{
ShowGridview();
}
private void ShowGridview()
{
DataSet ds = new DataSet();
sb = new StringBuilder();
sb.Append("SELECT * FROM tblregis");
objConn = new SqlConnection(DBConnection.strConnString);
if (objConn.State == ConnectionState.Closed)
{
objConn.Open();
}
objCmd = new SqlCommand();
objCmd.CommandText = sb.ToString();
objCmd.CommandType = CommandType.Text;
objCmd.Connection = objConn;
SqlDataAdapter da = new SqlDataAdapter(objCmd);
da.Fill(ds);
grvdata.DataSource = ds;
grvdata.DataBind();
}
}