如何在.net 6 winform项目中设置和连接数据库



我是Winform(.net 6(的新手,有人能给我展示一个如何在Winform中设置和连接数据库(SqlServer或MySQL(的示例代码吗?谢谢

这是通过C#访问SQL Server的begineers指南的链接。

的示例用法

在Winform 中设置并连接到数据库(SqlServer或MySQL(

我给你这里:

Form1.Designer

public partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox_dbAddress = new System.Windows.Forms.TextBox();
this.textBox_dbName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox_login = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox_password = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(180, 88);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(132, 15);
this.label1.TabIndex = 0;
this.label1.Text = "Database server address";
this.textBox_dbAddress.Location = new System.Drawing.Point(180, 106);
this.textBox_dbAddress.Name = "textBox_dbAddress";
this.textBox_dbAddress.Size = new System.Drawing.Size(188, 23);
this.textBox_dbAddress.TabIndex = 1;
this.textBox_dbName.Location = new System.Drawing.Point(180, 153);
this.textBox_dbName.Name = "textBox_dbName";
this.textBox_dbName.Size = new System.Drawing.Size(188, 23);
this.textBox_dbName.TabIndex = 3;
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(180, 135);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 15);
this.label2.TabIndex = 2;
this.label2.Text = "Database name";
this.textBox_login.Location = new System.Drawing.Point(180, 198);
this.textBox_login.Name = "textBox_login";
this.textBox_login.Size = new System.Drawing.Size(188, 23);
this.textBox_login.TabIndex = 5;
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(180, 180);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(37, 15);
this.label3.TabIndex = 4;
this.label3.Text = "Login";
this.textBox_password.Location = new System.Drawing.Point(180, 245);
this.textBox_password.Name = "textBox_password";
this.textBox_password.PasswordChar = '*';
this.textBox_password.Size = new System.Drawing.Size(188, 23);
this.textBox_password.TabIndex = 7;
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(180, 227);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(57, 15);
this.label4.TabIndex = 6;
this.label4.Text = "Password";
this.button1.Location = new System.Drawing.Point(180, 286);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 8;
this.button1.Text = "Connect";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox_password);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox_login);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox_dbName);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox_dbAddress);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private Label label1;
private TextBox textBox_dbAddress;
private TextBox textBox_dbName;
private Label label2;
private TextBox textBox_login;
private Label label3;
private TextBox textBox_password;
private Label label4;
private Button button1;
}

Form1

public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(
$"server={this.textBox_dbAddress.Text};" +
$"database={this.textBox_dbName.Text}; " +
$"user id={this.textBox_login.Text};" +
$"password={this.textBox_password.Text};" +             
$"Trusted_Connection=yes;" +
$"connection timeout=30");
try
{
myConnection.Open();
MessageBox.Show("Connection oppened");
}
catch
{
MessageBox.Show("Connection failed");
}
finally
{
myConnection.Close();
}
}
}

最新更新