如何在asp.net中连接数据库 [英] how to connect database in asp.net

查看:63
本文介绍了如何在asp.net中连接数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的问题是如何在asp.net中连接数据库sql server。



请一步一步轻松提供代码





谢谢,

Sandeep

Hi,

My question is how to connect database sql server in asp.net.

Please provide with code easily step by step


Thanks,
Sandeep

推荐答案

简短回答是:使用ADO.NET:

http://en.wikipedia.org/wiki/ADO.NET [ ^ ],

http:// msdn2。 microsoft.com/en-us/library/aa286484.aspx [ ^ ]。



这篇清晰简单的CodeProject文章可以帮助您立即入门:为初学者使用ADO.NET [ ^ ]。



-SA
Short answer is: use ADO.NET:
http://en.wikipedia.org/wiki/ADO.NET[^],
http://msdn2.microsoft.com/en-us/library/aa286484.aspx[^].

This clear and simple CodeProject article can help you to get started in no time: Using ADO.NET for beginners[^].

—SA


为了实用性和易于编辑,将连接设置存储到网站的WebConfig中的DB,如:



For usability, and ease of editing, store your connection settings to DB in WebConfig of the site like such:

<connectionStrings>
<add name="customNameForConnection" connectionString="Data Source=nameOfServer or IP;Initial Catalog=nameOfTable;Persist Security Info=True;User ID=Login;Password=Password" />
</connectionStrings>







using System.Data.SqlClient;
 lang="c#">public partial class ClassFile: System.Web.UI.Page
{
    SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["customCon"].ConnectionString);
    using(con){
            //using statement means the connection to DB will be closed once finished, meaning doesn't cause memory leaks.
        con.Open();
        string commandText = "SELECT * from DB";
        using (SqlCommand command = new SqlCommand(commandText, con))
        {
            using (SqlDataReader dataReader = command.ExecuteReader())
            {
               if(dataReader.Read())
               {
                  //read data
               }
            }
        }
    }
}





如果你有一个有条件的数据库查询,你可以使用commandParameters在commandString变量中使用更改变量,如下所示:





if you have a DB query with conditions, you can use commandParameters to utilise changing variables in the commandString variable like so:

var commandString = "SELECT * FROM DB where b = @id";





然后然后在usingSqlCommand br里面你要添加代码的ackets:



and then in then inside the usingSqlCommand brackets you would add the code:

command.Parameters.Add("@id", SqlDbType.Int).Value = nameOfVariable;



注意你可以拥有多个commandParameters。



希望这能为您提供连接SQL所需的帮助


Note you can have multiple commandParameters.

hope this gives you help you need in connecting to SQL


由于这是ASP.NET,您需要提及您正在使用的框架,如果您正在使用网页网站,然后您将使用以下代码连接到数据库。



Since this is ASP.NET, you need to mention the framework you're using, if you're using a Web Pages web site then you're going to use the following code to connect to the database.

Database.Open("databaseName"); // connected to databaseName





有关上述类和方法的更多信息,请阅读 MSDN文档 [ ^ ]。



其次,如果你使用任何其他方法,你可以简单地使用 SqlClient 命名空间。这暴露了可用于连接数据库并对其执行不同操作的方法和函数。由于它是Visual C#代码,因此您也可以在ASP.NET应用程序中使用它。您需要担心的是数据库的connectionString,因为它们需要精确,否则数据库将无法连接。有关connectionStrings的更多信息,请访问此网站。 http://connectionstrings.com [ ^ ]。这个的示例代码是,&b




For more on the above class and the methods, please read this MSDN document[^].

Secondly, if you're using any other method, you can simply just use the SqlClient namespace. That exposes the methods and functions that you can use to connect to the database and perform different operations on it. Since it is a Visual C# code, you can use it in ASP.NET application too. All you need to worry about here is the connectionString for your database, because they need to be precise otherwise the database won't be connected to. For more on connectionStrings, please visit this website. http://connectionstrings.com[^]. The sample code for this one would be,

using (SqlConnection conn = new SqlConnection("{connectionString}")) {
   conn.Open(); // connection opened to the database at the connectionString
   // perform operations here.
}





阅读这篇文章 [ ^ ]有关通过SqlClient命名空间连接到数据库的更多信息。它也适用于任何ASP.NET应用程序。



Read this article[^] for more on connecting to a database through a SqlClient namespace. It would work with any ASP.NET application too.


这篇关于如何在asp.net中连接数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆