protected void Page_Load(object sender, EventArgs e) { this.Master.HighlightMenu = "Customers"; //Declare the connection object SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString); //Make the connection Conn.Open(); //Define you query string sql = "SELECT * FROM Customer Order by LastName, FirstName"; //Declare a SQL Adapter SqlDataAdapter da = new SqlDataAdapter(sql, Conn); //Declare a DataTable DataTable dt = new DataTable(); //Populate the DataTable da.Fill(dt); //Bind the Listview lv.DataSource = dt; lv.DataBind(); dt.Dispose(); da.Dispose(); Conn.Close(); }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Configuration; public class DB { private SqlConnection sqlConn; public DB() { } public void OpenConnection() { if (sqlConn == null) { sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString); sqlConn.Open(); } else { sqlConn.Close(); } } public DataTable RunQuery(string Sql) { OpenConnection(); }
public class DB { public static SqlConnection Connect() { string connectionString = "Data Source=.;Initial Catalog = YourDatabaseName;uid =sa;pwd = YourPassword"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // Do something return connection; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)