C#WinForm下Form1连SQL登录后,如何让Form2、Form3等多窗体对数据库进行增删改查操作

i逐浪 i逐浪 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
C#WinForm下Form1连SQL登录后,如何让Form2、Form3等多窗体对数据库进行增删改查操作?

本人初学C#,
因技术有限,现在能做到的是一个窗体连接一次本窗体设置的SQL连接,
即Form1下的SQL查询使用Form1下的SQL登录配置,Form2下的SQL查询使用Form2下的SQL登录配置,Form3下的SQL查询使用Form3下的SQL登录配置。

现认为此方法设计与修改太累,也占窗体空间,想实现Form1登录窗口输入服务器数据库用户密码,或直接本地登录,再连接一次数据库,后面所有窗体都能调用这个数据库的打开方式,直接进行增删改查操作
窗体Form1
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.Data.SqlClient;

namespace FormTabNew
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public static string strCon = "";
public static string sqlServer = "";//用于Form3的数据库表名传递

private DataTable getTable(string str)//获取数据库清单
{
try
{
SqlConnection sqlcon = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("select name from sysdatabases ", sqlcon);
DataTable dt = new DataTable("sysdatabases");
da.Fill(dt);
return dt;
}
catch
{
return null;
}
}

private void Form1_Load(object sender, EventArgs e)
{
radioButton1.Checked = true;
}

private void button1_Click(object sender, EventArgs e) //打开选择服务器对话框
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
comboBox2.Text = Form2.strServer;//获取Form2中选中的服务器名称
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)//点击单选项“Windows身份登录”
{
textBox2.Enabled = textBox3.Enabled = false;
string str = "server=" + comboBox2.Text + ";database=master;Integrated Security=SSPI;";
comboBox1.DataSource = getTable(str);
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "name";
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)//点击单选项“SQL Server身份验证”
{
textBox2.Enabled = textBox3.Enabled = true;
textBox2.Focus();
}

private void button3_Click(object sender, EventArgs e)//点击按钮“登录”
{
if (radioButton1.Checked == true)//方案“Windows身份登录”
{
strCon = "Data Source=" + comboBox2.Text + ";Initial Catalog =" + comboBox1.Text + ";Integrated Security=SSPI;";
}
else if (radioButton2.Checked == true)//方案“SQL Server身份验证”
{
strCon = "Data Source=" + comboBox2.Text + ";Database=" + comboBox1.Text + ";Uid=" + textBox2.Text + ";Pwd=" + textBox3.Text + ";";
}
SqlConnection sqlcon = new SqlConnection(strCon);//更新参数strCon
try
{
sqlcon.Open();//数据库可以连接
sqlServer = comboBox1.Text;//给sqlServer赋值,使之等于comboBox1.Text

this.Hide();//当前窗体隐藏
Form3 frm3 = new Form3();
frm3.Show();//显示窗体Form3
}
catch
{
MessageBox.Show("用户或密码错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void button4_Click(object sender, EventArgs e)//点击按钮“取消”
{
Application.Exit();//全部退出结束窗体
}
}
}


窗体Form3
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;

namespace FormTabNew
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.sqlServer;//获取Form中的数据库名
}

private void Form3_FormClosing(object sender, FormClosingEventArgs e)//触发窗体关闭事件
{

}

}

//如何实现对当前获取的数据库名称进行增删改查操作?
}


是通过函数来实现的,还是哪种方式实现?能否提供一下相应的思路与脚本供我参考一下?谢谢!!!
...全文
评论 21
收藏 已收藏
分享
返回顶部
i逐浪
C# 111,129 成员 642,538 内容