更新的代碼的Visual Studio 2013 - 確認消息沒有私人無效
con.Open();
int rowsAffected = cmd.ExecuteNonQuery();
if(rowsAffected > 0)
MessageBox.Show("Record insertion succesful");
else
MessageBox.Show("Cannot insert a new Record");
cmd.ExecuteNonQuery();
con.Close();
我使用Visual Studio 2013,並創建一個Windows C#Web窗體應用程序,並已下添加一條記錄到數據庫的教程。它可以工作,但是我想在創建,編輯或刪除記錄後顯示確認消息。
在窗體控件我有:
private void Form1_Load(object sender, EventArgs e)
在它保護無效的教程。我不能添加:
If (Page.IsPostBake == true)
Label1.Text = ("Your record has been added to the database")
由於未定義頁面。我如何輸出確認信息?
cmd.Parameters.AddWithValue("@forename", textBox1.Text);
完整腳本:
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;
namespace somecityform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=MY-PC;Initial Catalog=somecity;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into somecity.staff (forename, surname, office, email) Values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=MY-PC;Initial Catalog=somecity;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"update somecity.staff SET forename='" + textBox1.Text + "', surname='" + textBox2.Text + "', office='"+ textBox3.Text +"', email='"+textBox4.Text +"' WHERE (staffnum= '" + textBox5.Text +"')", con);
cmd.ExecuteNonQuery();
con.Close();
}
private void label6_Click(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=MY-PC;Initial Catalog=somecity;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"delete from somecity.staff WHERE (staffnum= '" + textBox5.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
您正在關注WinForms的教程。 WinForms中沒有頁面。 Page和Page.IsPostBack屬於ASP.NET。 – Steve