示例示例首先在服务器上安装SQL Server,并创建一个数据库。在客户端机器上安装SQL Server的客户端组件,如SQL Server Studio,用于连接服务器。
1、首先在服务器上安装SQL Server,并创建一个数据库。
2、在客户端机器上安装SQL Server的客户端组件,如SQL Server Management Studio,用于连接服务器。
3、使用SQL Server Management Studio连接服务器,输入服务器IP地址、端口号、用户名和密码,即可连接服务器。
4、示例代码:
// 连接字符串
string connectionString = "Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=123456";
// 创建连接对象
SqlConnection conn = new SqlConnection(connectionString);
// 打开连接
conn.Open();
// 执行SQL语句
string sql = "SELECT * FROM Users";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
// 处理结果
while (reader.Read())
{
// 读取数据
string name = reader["Name"].ToString();
int age = Convert.ToInt32(reader["Age"]);
// 处理数据
Console.WriteLine("姓名:{0},年龄:{1}", name, age);
}
// 关闭连接
reader.Close();
conn.Close();
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(86条)