我想要C#的代码来连接asp.net和sql [英] i want code for c# for connecting asp.net and sql
本文介绍了我想要C#的代码来连接asp.net和sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用于连接asp.net和sql的C#代码是什么
what is the c# code for connecting asp.net and sql
推荐答案
尝试:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("SELECT iD, description FROM myTable", con))
{
using (SqlDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
int id = (int) reader["iD"];
string desc = (string) reader["description"];
Console.WriteLine("ID: {0}\n {1}", iD, desc);
}
}
}
}
大不了?
使用ADO.NET连接到SQL Server [ ^ ]
SQL Server和ADO.NET [
Big deal?
Connecting to SQL Server Using ADO.NET[^]
SQL Server and ADO.NET[^]
最简单的方法;
The simplest way of achieving this;
using System.Data.SqlClient;
using System.Data
Page_Load()
{
SQLConnection con = new SQLConnection();
SQLCommand cmd;
SQLDataAdapter adap = new SQLDataAdapter();
DataTable ds = new DataTable();
con.connectionstring = ""; //put whatever your connnection string to the db
con.open();
cmd = con.CreateCommand();
//If the command type is purely select statement
cmd.commandType = CommandType.Text;
cmd.CommandText = "select * from....";
//If the command type is a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_LoginDetailsProcess...."; //your stored procedure name
//If the command type is a stored procedure and has parameters to pass
cmd.Parameters.Clear();
cmd.parameters.addwithvalue("ParameterName", "ParameterValue");
adap.selectcommand = cmd;
adap.fill(ds);
con.Close();
}
如果在许多不同的地方使用,我建议您将其放入具有不同属性的单独类中.这将易于维护.
-欢呼
Nayan
I would recommend you to put this in to a separate class with different properties if using at many different places. This would be easy to maintain.
--Cheers
Nayan
这篇关于我想要C#的代码来连接asp.net和sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文