sale表
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 数据库连接
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "数据库连接";
string connString = "server=服务器名称;database =数据库名;uid=sa;pwd=自己的密码";
//建立连接对象
SqlConnection Sqlconn = new SqlConnection(connString);
//打开连接
Sqlconn.Open();
//为上面的连接指定Command对象
SqlCommand thiscommand = Sqlconn.CreateCommand();
thiscommand.CommandText = "select id from sale";
//为指定的command对象执行DataReader
SqlDataReader thisSqlDataReader = thiscommand.ExecuteReader();
while (thisSqlDataReader.Read())
{
Console.WriteLine("{0} ", thisSqlDataReader["id"]);
}
//关闭读取
thisSqlDataReader.Close();
//关闭连接
Sqlconn.Close();
Console.ReadLine();
}
}
}
数据库成功连接并执行操作结果: