插入和注册数据未显示在数据库表visual studio 2019中



我正在使用visual studio 2019和SQL server management studio 2008制作一个招生系统。当我试图点击插入按钮"插入成功",没有错误。当我试图点击注册按钮"记录更新成功",也没有错误。但是当我打开数据库并刷新数据库表时,数据表中没有数据。非常感谢对这个问题的任何支持。

private void button2_Click(object sender, EventArgs e)
{

try
{
//taking data from the GUI
string ID = textBox1.Text;
string RegistrationNumber = textBox1.Text;
string StudentName = textBox2.Text;
string DateOfBirth = dateTimePicker1.Text;
String Age = textBox3.Text;
String Gender;
if (radioButton1.Checked == true)
{
Gender = "Male";
}
else
{
Gender = "Female";
}
string ContactNumber = textBox4.Text;
;

if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "" && textBox4.Text == "")
{
MessageBox.Show("Complete the Missing Data");
}
else if (comboBox1.SelectedItem == null)
{
MessageBox.Show("Click on the selected item after selecting a course");
}
else
{
string course = (comboBox1.SelectedItem != null) ? comboBox1.SelectedItem.ToString() : "";
MessageBox.Show("Student Inserted Successfully!!");
string constr = (ConfigurationManager.ConnectionStrings["dbo.Table_1"] != null) ? ConfigurationManager.ConnectionStrings["dbo.Table_1"].ConnectionString : "";
connection = new SqlConnection("Data Source=.\(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
using (SqlConnection con = new SqlConnection(constr))
con.ConnectionString = constr;
}
if (con.State == ConnectionState.Closed)
con.Open();
SqlCommand com = new SqlCommand("INSERT INTO dbo.Table_1(ID, Registration Number, Student Name, Date of Birth, Age, Gender, Contact Number, Course Enrolled In) 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
{
MessageBox.Show("Error");
}
finally
{
con.Close();
}

private void button6_Click(object 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("Record Updated Successfully!!");
string constr = (ConfigurationManager.ConnectionStrings["dbo.Table_1"] != null) ? ConfigurationManager.ConnectionStrings["dbo.Table_1"].ConnectionString : "";
using (SqlConnection con = new SqlConnection(constr))
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", ID);
int Id;
if (!int.TryParse(textBox1.Text, out Id))
{
// Report problem to your user
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();
}

根据我的测试,我发现您定义了SqlCommand。

请尝试将您的代码修改如下。

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(object sender, EventArgs e)
{
try
{
string ID = textBox1.Text;
string StudentName = textBox2.Text;
DateTime DateOfBirth = dateTimePicker1.Value;
string constr = "sttr";
SqlConnection connection = new SqlConnection(constr);
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("success inserted");
connection.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}

}
private void button2_Click(object sender, EventArgs e)
{
string ID = textBox1.Text;
string constr = "str";
SqlConnection connection = new SqlConnection(constr);
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("Record Updated Successfully!!");
}

下一行

com。Connection = con;

添加下面代码

com.executenonquery ();

相关内容

  • 没有找到相关文章

最新更新