如何用c#代码创建sql连接,访问sql server然后有条件地重定向?

这是一位经验丰富的初学者提出的问题!

使用ASP.NET 4 C#AND SQL服务器,

我在web.config中有一个名为“myCS”的myDatabase的连接字符串。 我有一个名为myDB的数据库。 我有一个名为myTable的表,其主键名为myPK

什么是创建SQL连接背后的最小代码行(最小代码),然后从myTable中选择myPK ==“simpleText”

它可能包括:

sqlconnection conn = new sqlconnection(??? myCS) string SQLcommand = select * from myDB.myTable where myPK==myTestString; sqlCommand command = new SqlCommand(SQL,conn); conn.Open(); booleanFlag = ???? conn.Close(); conn.Dispose(); 

然后

 If ( theAnswer != NULL ) // or (if flag) { Response.Redirect("Page1.aspx"); } else { Response.Redirect("Page2.aspx"); } 

这是一个有限的简单教程:

首先,你想要一个class级为你做艰苦的工作,然后你会轻松使用它。

首先,您必须在web.config文件中创建连接字符串并为其命名。 这里它被命名为DatabaseConnectionString ,但您可以根据问题的要求将其命名为myCS

现在,在App_Code中创建一个新的类文件,并将其命名为SqlComm (这只是一个示例名称),如:

 using System; using System.Data; using System.Data.SqlClient; using System.Web; public class SqlComm { // this is a shortcut for your connection string static string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConStr"].ConnectionString; // this is for just executing sql command with no value to return public static void SqlExecute(string sql) { using (SqlConnection conn = new SqlConnection(DatabaseConnectionString)) { SqlCommand cmd = new SqlCommand(sql, conn); cmd.Connection.Open(); cmd.ExecuteNonQuery(); } } // with this you will be able to return a value public static object SqlReturn(string sql) { using (SqlConnection conn = new SqlConnection(DatabaseConnectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); object result = (object)cmd.ExecuteScalar(); return result; } } // with this you can retrieve an entire table or part of it public static DataTable SqlDataTable(string sql) { using (SqlConnection conn = new SqlConnection(DatabaseConnectionString)) { SqlCommand cmd = new SqlCommand(sql, conn); cmd.Connection.Open(); DataTable TempTable = new DataTable(); TempTable.Load(cmd.ExecuteReader()); return TempTable; } } // sooner or later you will probably use stored procedures. // you can use this in order to execute a stored procedure with 1 parameter // it will work for returning a value or just executing with no returns public static object SqlStoredProcedure1Param(string StoredProcedure, string PrmName1, object Param1) { using (SqlConnection conn = new SqlConnection(DatabaseConnectionString)) { SqlCommand cmd = new SqlCommand(StoredProcedure, conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter(PrmName1, Param1.ToString())); cmd.Connection.Open(); object obj = new object(); obj = cmd.ExecuteScalar(); return obj; } } } 

好的,这只是一个类,现在您应该知道如何使用它:

如果您希望执行删除,插入,更新等命令,请使用以下命令:

 SqlComm.SqlExecute("TRUNCATE TABLE Table1"); 

但如果您需要从数据库中检索特定值,请使用以下命令:

 int myRequiredScalar = 0; object obj = new object(); obj = SqlComm.SqlReturn("SELECT TOP 1 Col1 FROM Table1"); if (obj != null) myRequiredScalar = (int)obj; 

您可以通过这种方式从数据库中检索一堆行(其他方式与其他方式一样)这与您的特定问题相关

 int Col1Value = 0; DataTable dt = new DataTable(); dt = SqlComm.SqlDataTable("SELECT * FROM myTable WHERE myPK='simpleText'"); if (dt.Rows.Count == 0) { // do something if the query return no rows // you may insert the relevant redirection you asked for } else { // Get the value of Col1 in the 3rd row (0 is the first row) Col1Value = (int)dt.Rows[2]["Col1"]; // or just make the other redirection from your question } 

如果您需要执行存储过程,无论是否返回值,这都是这样做的方法(在此示例中没有返回值)

 SqlComm.SqlStoredProcedure1Param("TheStoredProcedureName", "TheParameterName", TheParameterValue); 

同样,对于您的特定问题,使用SqlDataTable返回表,如果dt.Rows.Count >0重定向

玩得开心。

有很多方法: LINQ , SqlDataReader , SQLDataAdapter ,根据你想要读取的内容(单值,数据表……),所以这里有一个例子:

 using (SqlConnection con = new SqlConnection("SomeConnectionString")) { var cmd = new SqlCommand("select from myTable where myPK==N'"+ simpleText+ "'",con); cmd.Connection.Open(); var sqlReader = cmd.ExecuteReader(); while(sqlReader.Read()) { //Fill some data like : string result = sqlReader("SomeFieldName"); } sqlReader.Close(); cmd.Connection.Close(); cmd.Dispose(); }