0

SQL Server 데이터베이스에 데이터를 삽입하고 업데이트하는 데 문제가 있습니다. 데이터를 선택할 수 있습니다. Visual Studio 2012, SQL Server 2012를 사용하고 있습니다.SQL 서버에 업데이트 및 삽입 할 수 없지만 데이터를 선택할 수 있습니다.

제발 도와 줘서 고마워.

이것은의 app.config 난 당신이 사용자 권한을 제공 할 필요가 있다고 생각

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Configuration; 
using System.Data; 
using System.Drawing;`enter code here` 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

namespace ProjectAppWIn 
{ 
    public partial class Refill : Form 
    { 
     ProjectAppWIn.Login.user s; 
     ProjectAppWIn.Home.userr r; 
     public string sa; 
     public string se; 
     public Refill(ProjectAppWIn.Login.user s1, ProjectAppWIn.Home.userr s2)  //string user) 
     { 
      InitializeComponent(); 
      s = s1; 
      // label2.Text = "Welcome : " + " " + (user); 
      sa = s.name; 
      //on which control you want to show the username.... 
      label2.Text = "Welcome..." + s.name; 

      r = s2; 
      se = r.id; 
      textBox8.Text = r.id + ""; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      this.Hide(); 

      Form targetform = new Login(); 
      targetform.Show(); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      using (SqlConnection con1 = new SqlConnection("Data Source=KHUNP\\SQLEXPRESS;Initial Catalog=kmuttssc;User ID=sa;Password=db2admin;")) 
      { 
       if (textBox1.Text.Contains("g") || textBox1.Text.Contains("G") == true) 
       { 
        DataTable dte = new DataTable(); 
        con1.Open(); 
        SqlDataReader myRead = null; 
        //SqlCommand myCommand = new SqlCommand("select * from card,user where card.card_id='" + textBox1.Text + "'", con1); 
        SqlCommand myCom = new SqlCommand("select card_balance,card_id from card where guest_id = '" + textBox1.Text + "'", con1); 

        myRead = myCom.ExecuteReader(); 

        while (myRead.Read()) 
        { 
         textBox6.Text = (myRead["card_balance"].ToString()); 
         textBoxcardid.Text = (myRead["card_id"].ToString()); 

         //TextBox8.Text = (myReader[].ToString()); 

         //DropDownListGender.SelectedItem.Text = (myReader["gender"].ToString()); 
         //DropDownListMonth.Text = (myReader["birth"].ToString()); 
         //DropDownListYear.Text = (myReader["birth"].ToString()); 
         //TextBoxAddress.Text = (myReader["address"].ToString()); 
         //TextBoxCity.Text = (myReader["city"].ToString()); 
         //DropDownListCountry.SelectedItem.Text = (myReader["country"].ToString()); 
         //TextBoxPostcode.Text = (myReader["postcode"].ToString()); 
         //TextBoxEmail.Text = (myReader["email"].ToString()); 
         //TextBoxCarno.Text = (myReader["carno"].ToString()); 
        } 
        con1.Close(); 

        //textBox5.Text = string.Empty; 
        //textBox7.Text = string.Empty; 
        // *****textBox8.Text = Session["id"] + ""; 
       } 
       else 
       { 
        DataTable dt = new DataTable(); 
        con1.Open(); 
        SqlDataReader myReader = null; 
        //SqlCommand myCommand = new SqlCommand("select * from card,user where card.card_id='" + textBox1.Text + "'", con1); 
        SqlCommand myCommand = new SqlCommand("select u.user_id, u.user_fname, u.user_lname, c.user_id, c.card_balance,c.card_id from [user] u JOIN [card] c ON u.user_id = c.user_id where c.user_id = '" + textBox1.Text + "'", con1); 

        myReader = myCommand.ExecuteReader(); 

        while (myReader.Read()) 
        { 
         textBox6.Text = (myReader["card_balance"].ToString()); 
         textBox2.Text = (myReader["user_fname"].ToString()); 
         textBox3.Text = (myReader["user_lname"].ToString()); 
         textBoxcardid.Text = (myReader["card_id"].ToString()); 

        } 

        con1.Close(); 


        textBox5.Text = string.Empty; 
        textBox7.Text = string.Empty; 
        label9.Text = string.Empty; 
        // ****textBox8.Text = Session["id"] + ""; 
       }//end using 
      } 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      textBox7.Text = (Convert.ToInt32(textBox5.Text) + Convert.ToInt32(textBox6.Text)).ToString(); 
      using (SqlConnection con1 = new SqlConnection("Data Source=KHUNP\\SQLEXPRESS;Initial Catalog=kmuttssc;User ID=sa;Password=db2admin;")) 

       { 
        if (textBox1.Text.Contains("g") || textBox1.Text.Contains("G") == true) 
        { 
         DataTable dt = new DataTable(); 
         con1.Open(); 
         SqlDataReader myReader = null; 
         //SqlCommand myCommand = new SqlCommand("select * from card,user where card.card_id='" + TextBox1.Text + "'", con1); 
         SqlCommand myCommand = new SqlCommand("UPDATE card c join guest g on c.guest_id = g.guest_id SET c.card_balance = @card_balance,[email protected]_status WHERE c.guest_id = '" + textBox1.Text + "'", con1); 
         myCommand.Parameters.Add("@card_balance", System.Data.SqlDbType.SmallInt); 
         //myCommand.Parameters.Add("@staff_id", System.Data.SqlDbType.SmallInt); 
         myCommand.Parameters["@card_balance"].Value = textBox7.Text; 
         //myCommand.Parameters["@staff_id"].Value = textBox8.Text; 
         myCommand.Parameters.AddWithValue("@guest_status", textBox9.Text); 

         //myCommand.Parameters["@staff_id"].Value = Session["]; 

         try 
         { 
          myCommand.ExecuteNonQuery(); 
          //TextBox1.Text = string.Empty; 
          //TextBox2.Text = string.Empty; 
          //TextBox3.Text = string.Empty; 
          //TextBox5.Text = string.Empty; 
          //TextBox6.Text = string.Empty; 


          using (SqlConnection conn = new SqlConnection("Data Source=KHUNP\\SQLEXPRESS;Initial Catalog=kmuttssc;User ID=sa;Password=db2admin;")) 
          { 
           SqlCommand cmd = new SqlCommand("INSERT INTO transactionc (tranc_total, card_id,staff_id,date) VALUES (@tranc_total, @staff_id,@card_id, @date)"); 
           cmd.CommandType = CommandType.Text; 
           cmd.Connection = conn; 
           cmd.Parameters.AddWithValue("@tranc_total", textBox5.Text); 
           cmd.Parameters.AddWithValue("@card_id", textBoxcardid.Text); 
           cmd.Parameters.AddWithValue("@staff_id",textBox8.Text); 
           cmd.Parameters.AddWithValue("@date", DateTime.Now); 
           //cmd.Parameters.AddWithValue("@Address", txtAddress.Text); 
           conn.Open(); 
           cmd.ExecuteNonQuery(); 
          } 




          label9.Text = "<b><big><big> Complete !!!</big></big> </b>"; 


         } 
         catch 
         { 
          textBox7.Text = string.Empty; 

          label9.Text = "<b> <big> <big> Not Complete!!!</big> </big> </b>"; 
         } 
         finally 
         { 
          con1.Close(); 
         } 

         //myCommand.Parameters.AddWithValue("@card_balance", TextBox7.Text); 
         //myCommand.ExecuteNonQuery(); 


        } 
        else 
        { 
         DataTable dt = new DataTable(); 
         con1.Open(); 
         SqlDataReader myReader = null; 
         //SqlCommand myCommand = new SqlCommand("select * from card,user where card.card_id='" + TextBox1.Text + "'", con1); 
         SqlCommand myCommand = new SqlCommand("UPDATE card set [email protected]_balance , WHERE user_id = '" + textBox1.Text + "'", con1); 
         myCommand.Parameters.Add("@card_balance", System.Data.SqlDbType.SmallInt); 
         //myCommand.Parameters.Add("@staff_id", System.Data.SqlDbType.SmallInt); 
         myCommand.Parameters["@card_balance"].Value = textBox7.Text; 
         //myCommand.Parameters["@staff_id"].Value = textBox8.Text; 
         //myCommand.Parameters.AddWithValue("@guest_status", TextBox9.Text); 

         //myCommand.Parameters["@staff_id"].Value = Session["]; 

         try 
         { 
          myCommand.ExecuteNonQuery(); 
          //TextBox1.Text = string.Empty; 
          //TextBox2.Text = string.Empty; 
          //TextBox3.Text = string.Empty; 
          //TextBox5.Text = string.Empty; 
          //TextBox6.Text = string.Empty; 

          using (SqlConnection conn = new SqlConnection("Data Source=KHUNP\\SQLEXPRESS;Initial Catalog=kmuttssc;User ID=sa;Password=db2admin;")) 
          { 
           SqlCommand cmd = new SqlCommand("INSERT INTO transactionc (tranc_total, card_id,staff_id, date) VALUES (@tranc_total, @card_id,@staff_id, @date)"); 
           cmd.CommandType = CommandType.Text; 
           cmd.Connection = conn; 
           cmd.Parameters.AddWithValue("@tranc_total", textBox5.Text); 
           cmd.Parameters.AddWithValue("@card_id", textBoxcardid.Text); 
           cmd.Parameters.AddWithValue("@staff_id", textBox8.Text); 
           cmd.Parameters.AddWithValue("@date", DateTime.Now); 
           //cmd.Parameters.AddWithValue("@Address", txtAddress.Text); 
           conn.Open(); 
           cmd.ExecuteNonQuery(); 
          } 

          label9.Text = "<b><big><big> Complete !!!</big></big> </b>"; 


         } 
         catch 
         { 
          textBox7.Text = string.Empty; 

          label9.Text = "<b> <big> <big> Not Complete!!!</big> </big> </b>"; 
         } 
         finally 
         { 
          con1.Close(); 
         } 



       } 






      } 
     } 

     private void button5_Click(object sender, EventArgs e) 
     { 
      Form targetform = new Return(s, r); 
      targetform.Show(); 
      this.Hide(); 
     } 

     private void button6_Click(object sender, EventArgs e) 
     { 
      Form targetform = new Home1(s, r); 
      targetform.Show(); 
      this.Hide(); 
     } 


    } 
} 
+0

오류 메시지가 무엇입니까? – romanoza

+0

조용히 죽을까요? DB 전화는 어떻게합니까? 나는 [mvce] (https://stackoverflow.com/help/mcve)가 도움이 될 것이라고 생각한다. – kkuilla

+0

오류가 있습니까? 그렇다면 오류 메시지는 무엇입니까? 또한이 애플리케이션을 구조화 한 방식은 꽤 끔찍하고 유지 보수가 쉽지 않습니다. 'textBox7'와'label9'보다 더 의미있는 컨트롤 이름을 주어야하고 UI 요소들과 DB 요소들을 분리시켜야합니다. –

답변

2

내 ConnectionString을합니다. 데이터베이스로 이동하여 아래의 쿼리를 실행 : -

USE [DBName] 
GO 
EXEC sp_addrolemember N'db_datawriter', N'UserName' 
GO 
EXEC sp_addrolemember N'db_datareader', N'UserName' 

GRANT privilage를 사용하여 인 permsion을 제공하는 또 하나 개의 방법이있다.

+0

미안하지만, 내가 뭘해야하는지 이해가 안돼. –

+0

@ Khun'pCcm : - SQL Management Studio로 이동하여 위의 쿼리를 데이터베이스에서 실행하고 사용자 이름은 –

+0

입니다. 오류입니다. 메시지 15410, 수준 11, 상태 1, 프로 시저 sp_addrolemember, 줄 35 이 데이터베이스에는 'sa'사용자 또는 역할이 없습니다. 메시지 15410, 수준 11, 상태 1, 프로 시저 sp_addrolemember, 줄 35 이 데이터베이스에는 'sa'사용자 또는 역할이 없습니다. –

관련 문제