Last active
December 28, 2020 20:13
-
-
Save boazcstrike/d5172c64c426cf186f04bea43f1c78a3 to your computer and use it in GitHub Desktop.
SQL server connection in C# asp.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data.SqlClient; | |
namespace Eth4You{ | |
public partial class miner_test : System.Web.UI.Page{ | |
public void test() | |
{ | |
string connString = "Server=PCNAME\\SQLEXPRESS;Database=DB_name;User Id=username;Password=yourpassword;"; | |
/* if your using Windows Authentication, use the code below | |
string connString = "Server=PCNAME\\SQLEXPRESS;Database=DB_name;Integrated Security=SSPI;"; | |
// I first opened and created the connection to the server */ | |
SqlConnection conn = new SqlConnection(connString); | |
conn.Open(); | |
//Then create and bind the sql command through the connection | |
string cmdString = "SELECT * FROM table_name"; | |
SqlCommand cmd = new SqlCommand(cmdString, conn); | |
cmd.CommandType = System.Data.CommandType.Text; | |
cmd.Connection = conn; | |
//test if the connection can read and write the data in the table | |
var tempStr = ""; | |
SqlDataReader reader = cmd.ExecuteReader(); | |
while (reader.Read()) //while there is data being read | |
{ | |
tempStr += "<br>IIDD:"; | |
tempStr += reader["IIDD"].ToString(); // store the string to tempStr | |
tempStr += "<br>name:"; | |
tempStr += reader["name"].ToString(); // store the string to tempStr | |
tempStr += "<br /><br>"; // store a space to break data | |
} | |
//Output the data to the ID=Label in miner_test.aspx | |
temp.Text = tempStr; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment