Link the database and add data to the WinForm window
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;
Using System.Data.SqlClient; It's important to use namespaces
Namespace Freepractice
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
private void Butaddcourse_click (object sender, EventArgs e)
{
Get the value of a text box
string coursenmae = This.textCourseName.Text;
string coursebyname = This.textByName.Text;
Get the value of the Password text box
string passworld = This.textPassword.Text;
Create a database link string
String connstring = "server=.; database=studentmisdb;uid=sa;pwd=123456 ";
Create a database Connection object
SqlConnection con = new SqlConnection (connstring);
Con. Open ();
The SQL statement to execute
String sql = string. Format ("INSERT into Course values (' {0} ', ' {1} ')", Coursenmae, Coursebyname);
To create a SQL command object
SqlCommand cmd = new SqlCommand (Sql,con);
Determine if the database is open
if (Con. State==connectionstate.open)
{
Perform an update receipt. Returns an int type value
int i = cmd. ExecuteNonQuery ();
Determines whether the return value is bit 0
if (i==0)
{
MessageBox.Show ("modification failed");
}
Else
{
MessageBox.Show ("modified successfully");
}
//
if (Con. State==connectionstate.open)
{
Close Database Link
Con. Close ();
}
}
}
}
}
Insert operations for C # student tables