关于数据库连接和在WINFROM下做用户登录

开学第一天,应该给自己打气的,但是说的再多也没用,以后走着瞧吧
以下是今天的收获

 

数据库的连接

 

混合身份登录:

Data Source=本机名\SQLSERVER2008;Initial Catalog=要连接的数据库名字;Persist Security Info=True;User ID=sa;Password=自己的密码 

 

windows登录:datasource=.\SQLSERVER2008;database=csdnnews;integrated security=true 

 

连接SQLExpress

Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|

\Database1.mdf;Integrated Security=True;UserInstance=True

 

WinForm做用户登录

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Configuration;

using System.Data.SqlClient;

 

namespace 复习_用户登录

{

    public

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在WinForm中连接MySQL数据库,需要使用MySQL连接器/驱动程序。以下是连接WinForm和MySQL数据库的步骤: 1. 下载MySQL连接器/驱动程序:您可以从MySQL官方网站下载MySQL Connector/NET。 2. 将MySQL连接器/驱动程序添加到项目中:将MySQL连接器/驱动程序添加到WinForm项目中。您可以通过NuGet包管理器来添加MySQL连接器/驱动程序。 3. 在WinForm中添加连接字符串:添加连接MySQL数据库所需的连接字符串。连接字符串包括MySQL服务器名称、用户名、密码、数据库名称等信息。 4. 创建连接对象:在WinForm中创建MySQL连接对象,并使用连接字符串初始化它。 5. 打开连接:使用连接对象打开与MySQL数据库的连接。 6. 执行SQL查询:在WinForm中执行需要执行的SQL查询,如SELECT、INSERT、UPDATE和DELETE语句。 7. 关闭连接:使用连接对象关闭与MySQL数据库的连接。 以下是一个连接WinForm和MySQL数据库的示例代码: ```csharp using System; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace WinFormMySQLConnection { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnConnect_Click(object sender, EventArgs e) { // 添加连接字符串 string connectionString = "server=localhost;database=testdb;uid=root;password=123456"; // 创建连接对象 MySqlConnection connection = new MySqlConnection(connectionString); try { // 打开连接 connection.Open(); // 执行SQL查询 MySqlCommand command = new MySqlCommand("SELECT * FROM users", connection); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // 处理查询结果 Console.WriteLine(reader["id"] + " " + reader["name"]); } // 关闭连接 reader.Close(); connection.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } ``` 在此示例中,我们通过单击按钮连接到MySQL数据库,并执行SELECT语句来检索用户表中的数据。请注意,此示例仅用于演示目的。在实际应用程序中,您需要更多的错误处理和安全性检查。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值