要在Winform窗体中连接数据库,您需要执行以下步骤:
引用数据库相关的命名空间:using System.Data.SqlClient;创建数据库连接对象:SqlConnection connection = new SqlConnection("连接字符串");打开数据库连接:connection.Open();执行数据库操作:// 创建数据库命令对象SqlCommand command = new SqlCommand();command.Connection = connection;// 设置要执行的SQL语句command.CommandText = "SELECT * FROM 表名";// 执行查询,并获取结果集SqlDataReader reader = command.ExecuteReader();// 遍历结果集while (reader.Read()){// 读取每一行数据string column1 = reader.GetString(0);int column2 = reader.GetInt32(1);// 其他列以此类推}关闭数据库连接和释放资源:reader.Close();connection.Close();请注意,上述示例中的连接字符串需要替换为您自己的实际连接字符串,以便连接到您的数据库。另外,还可以根据实际需求设置和执行其他数据库操作,例如插入、更新和删除数据等。

