System.Data.SqlClient
,然后创建SqlConnection
对象,设置连接字符串,最后调用Open
方法打开连接。csharp,using System;,using System.Data.SqlClient;,,public partial class _Default : System.Web.UI.Page,{, protected void Page_Load(object sender, EventArgs e), {, string connectionString = "Data Source=your_server_name;Initial Catalog=your_database_name;User ID=your_username;Password=your_password";, using (SqlConnection con = new SqlConnection(connectionString)), {, try, {, con.Open();, // 执行数据库操作, }, catch (Exception ex), {, Response.Write("Error: " + ex.Message);, }, }, },},
`,,请将
your_server_name、
your_database_name、
your_username和
your_password`替换为实际的数据库信息。csharp,using System;,using System.Data.SqlClient;,,public class DatabaseConnection,{, private string connectionString = "your_connection_string_here";,, public void ConnectToDatabase(), {, using (SqlConnection connection = new SqlConnection(connectionString)), {, try, {, connection.Open();, Console.WriteLine("Connected to the database successfully.");, }, catch (Exception ex), {, Console.WriteLine("Error: " + ex.Message);, }, }, },},
``Powered By Z-BlogPHP 1.7.3
Copyright Your WebSite.Some Rights Reserved.