我正在使用visual studio 2019和SQL server management studio 2008制作一個招生系統。當我試圖點擊插入按鈕'Inserted Successfully'時,沒有任何錯誤。當我試圖點擊注冊按鈕'Record Updated Successfully'時,也沒有任何錯誤。但當我打開資料庫并重繪 資料庫表時,資料表中沒有資料。
private void button2_Click(物件 sender, EventArgs e)
{
try
{
//從GUI中獲取資料。
string ID = textBox1.Text。
string RegistrationNumber = textBox1.Text;
string StudentName = textBox2.Text;
string DateOfBirth = dateTimePicker1.Text;
String Age = textBox3.Text。
字串性別。
if (radioButton1.Checked == true)
{
性別 = "Male"。
}
else; }
{
性別 = "女性"。
}
string ContactNumber = textBox4.Text。
;
if (textBox1.Text == " && textBox2. Text == "" && textBox3.Text == " && textBox4.Text == ")
{
MessageBox.Show("完成缺少的資料")。
}
else if (comboBox1.SelectedItem == null)
{
MessageBox.Show("選擇課程后點擊所選專案")。
}
else; }
{
string course = (comboBox1. SelectedItem != null) ? comboBox1.SelectedItem.ToString() : ""。
MessageBox.Show("學生成功插入!!")。
string constr = (ConfigurationManager.ConnectionStrings["dbo.Table_1"] !=null) ? ConfigurationManager.ConnectionStrings["dbo.Table_1"].ConnectionString : ""。
connection = new SqlConnection("Data Source=. .(localdb)MSSQLLocalDB;初始目錄=master;集成安全=True;連接超時=30;加密=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False")。)
using (SqlConnection con = new SqlConnection(constru))
con.ConnectionString = constr;
}
if (con.State == ConnectionState.Closed)
con.Open()。
SqlCommand com = new SqlCommand("INSERT INTO dbo. Table_1(ID,注冊號,學生姓名,出生日期,年齡,性別,聯系電話,注冊課程) VALUES(@ID,@RegistrationNumber,@StudentName,@DateOfBirth,@Age,@Gender,@ContactNumber)", connection)。)
com.CommandType = CommandType.Text;
com.Connection = con;
com.CommandText = "SELECT * FROM dbo.Table_1 WHERE ID = @ID;"。
com.Parameters.AddWithValue("@ID", textBox1.Text)。
com.Parameters.AddWithValue("@RegistrationNumber", textBox1.Text)。
com.Parameters.AddWithValue("@StudentName", textBox2.Text);
com.Parameters.AddWithValue("@DateOfBirth", dateTimePicker1.Text);
com.Parameters.AddWithValue("@Age", textBox3.Text);
com.Parameters.AddWithValue("@Gender", Gender);
com.Parameters.AddWithValue("@ContactNumber", textBox4.Text)。
com.ExecuteNonQuery()。
com.ExecuteReader();
com.Dispose()。
}
catch"Error")。
}
finally; }
{
con.Close()。
}
private void button6_Click(物件sender, EventArgs e)
{
string ID = textBox1.Text。
if (ID == null) ;
if (textBox1.Text=="" || textBox2. Text=="" || textBox3.Text=="" || textBox4.Text=="")
{
MessageBox.Show("Please Enter Missing Details")。
}
else; }
{
MessageBox.Show("記錄成功更新!!")。
string constr = (ConfigurationManager.ConnectionStrings["dbo.Table_1"] !=null) ? ConfigurationManager.ConnectionStrings["dbo.Table_1"].ConnectionString : ""。
using (SqlConnection con = new SqlConnection(constru))
con.ConnectionString = constr;
if(con.State==ConnectionState.Closed)
{
con.Open()。
}
String sql = "SELECT COUNT(*) AS [Count] FROM dbo.Table_1 WHERE ID =@ID" ;
SqlCommand cmd = new SqlCommand(sql, con)。
cmd.Parameters.AddWithValue("@ID"/span>, ID)。
int Id。
if (!int.TryParse(textBox1.Text,out Id))
{
//向用戶報告問題。
return;
}
SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)。
while (sdr.Read())
{
if (Convert.ToInt32(sdr["count"]) == 1)
{
button2.Enabled = false;
button1.Enabled = true;
}
else; }
{
button2.Enabled = true;
button1.Enabled = false;
}
}
{
}
}
con.Close()。
}
uj5u.com熱心網友回復:
在下面一行之后
com.connection = con;
添加下面的代碼
com.executenonquery();
uj5u.com熱心網友回復:
根據我的測驗,我發現你定義了SqlCommand.CommandText兩次。
請嘗試將你的代碼修改為以下內容。
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "insert into Student(ID,StudentName,DateOfBirth)values(@ID,@StudentName,@DateOfBirth)" ;
command.Parameters.AddWithValue("@ID", Convert.ToInt32(ID))。
command.Parameters.AddWithValue("@StudentName", textBox2.Text)。
command.Parameters.AddWithValue("@DateOfBirth", DateOfBirth
);
另外,請注意,我們應該把MessageBox.Show放在代碼com.ExecuteNonQuery();之后。
下面是一個代碼例子,你可以參考一下,根據我的測驗,它運行良好。
private void button1_Click(物件sender, EventArgs e)
{
try
{
string ID = textBox1.Text。
string StudentName = textBox2.Text;
DateTime DateOfBirth = dateTimePicker1.Value;
string constr = "sttr"/span>;
SqlConnection connection = new SqlConnection(constru)。
connection.Open()。
SqlCommand command = new SqlCommand() 。
command.Connection = connection;
command.CommandText = "insert into Student(ID,StudentName,DateOfBirth)values(@ID,@StudentName,@DateOfBirth)" ;
command.Parameters.AddWithValue("@ID", Convert.ToInt32(ID))。
command.Parameters.AddWithValue("@StudentName", textBox2.Text)。
command.Parameters.AddWithValue("@DateOfBirth", DateOfBirth)。
command.ExecuteNonQuery();
MessageBox.Show("成功插入")。
connection.Close()。
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString())。
}
}
private void button2_Click(物件sender, EventArgs e)
{
string ID = textBox1.Text。
string constr = "str"/span>;
SqlConnection connection = new SqlConnection(constru)。
connection.Open()。
SqlCommand command = new SqlCommand() 。
command.Connection = connection;
command.CommandText = "SELECT COUNT(*) AS [Count] FROM Student WHERE ID =@ID" 。
command.Parameters.AddWithValue("@ID", Convert.ToInt32(ID))。
SqlDataReader sdr = command.ExecuteReader(CommandBehavior.CloseConnection)。
while (sdr.Read())
{
if (Convert.ToInt32(sdr["count"]) == 1)
{
button2.Enabled = false;
button1.Enabled = true;
}
else; }
{
button2.Enabled = true;
button1.Enabled = false;
}
}
MessageBox.Show("記錄成功更新!!")。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314781.html
標籤:
