I'm new to programming so please bear with me as I learn. I've been looking for days for a way to make this work, trying different solutions that I have found on here and other sites. I am using user input to create my connection string, and button 1 works great to verify that a connection has been established, button 2 not so much. I am trying to create a button that once pushed will execute a SQL command and provide the results from the command.
This is what I have so far, its button 2 that I have not been able to get to work yet.
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();
}
}
}
I am trying to get the button to run this sql:
select count(*) from history
select count(*) from results where communicated = 0
I can run the SQL Query in SSMS no problem its just getting it to launch from the GUI I'm creating.
Any help is greatly appreciated.