我是编程新手,所以请耐心等待我学习。我一直在寻找一种方法来完成这项工作,尝试我在这里和其他网站上找到的不同解决方案。我正在使用用户输入来创建我的连接字符串,并且按钮 1 可以很好地验证连接是否已建立,按钮 2 则不然。我正在尝试创建一个按钮,一旦按下它将执行 SQL 命令并提供命令的结果。
这是我到目前为止所拥有的,它的按钮 2 我还不能开始工作。
using Microsoft.AspNet.SignalR.Infrastructure;
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
{
string ServerName = textBox1.Text;
string Database = textBox2.Text;
string Username = textBox3.Text;
string Pass = textBox4.Text;
string connetionString;
SqlConnection cnn;
connetionString = @"Data Source= " + ServerName + ";Initial Catalog= " + Database + ";User ID=" + Username + ";Password= " + Pass + ";";
cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show("Connection Open !");
cnn.Close();
}
catch (Exception) { MessageBox.Show("Login Failed, Information is Incorrect"); }
}
}
private void button2_Click(object sender, EventArgs e)
{
string ServerName = textBox1.Text;
string Database = textBox2.Text;
string Username = textBox3.Text;
string Pass = textBox4.Text;
string results = textBox5.Text;
string connetionString;
SqlConnection cnn;
connetionString = @"Data Source= " + ServerName + ";Initial Catalog= " + Database + ";User ID=" + Username + ";Password= " + Pass + ";";
string userInput = "";
var process = new Process();
var startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = string.Format(@"Data Source= " + ServerName + ";Initial Catalog= " + Database + ";User ID=" + Username + ";Password= " + Pass + "; "" SELECT count(*) from participanthistory, SELECT count(*) from postransaction where communicated = 0, userInput);
process.StartInfo = startInfo;
process.Start();
}
}
}
我正在尝试获取运行此 sql 的按钮:
select count(*) from history
select count(*) from results where communicated = 0
我可以在 SSMS 中运行 SQL 查询没问题,它只是让它从我正在创建的 GUI 启动。
任何帮助是极大的赞赏。