2014-05-10 48 views
0

更新的代碼的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(); 
     } 
    } 
} 
+0

您正在關注WinForms的教程。 WinForms中沒有頁面。 Page和Page.IsPostBack屬於ASP.NET。 – Steve

回答

0

的ExecuteNonQuery返回表示被插入,修飾或由命令刪除的行的數量的整數值。在你的情況下,你只需要添加

con.Open(); 
int rowsAffected = cmd.ExecuteNonQuery(); 
if(rowsAffected > 0) 
    MessageBox.Show("Record insertion succesful"); 
else 
    MessageBox.Show("Cannot insert a new Record"); 
con.Close(); 
+0

你錯過了幾個括號。我已更新腳本以顯示我如何添加您的代碼。我得到這個錯誤: 「在System.Data.dll中發生了'System.Data.SqlClient.SqlException'類型的未處理異常」 – raybarone

+0

嘗試查看異常詳細信息。特別是對於異常。消息值。它可以解釋爲什麼查詢失敗 – Steve

+0

{「字符串或二進制數據將被截斷。\ r \ n語句已被終止。」} – raybarone