2013-11-21 1 views
0

정보를 데이터베이스로 이동하기 위해 텍스트 필드에 삽입 할 수 없습니다. 여기 내가 지금까지 가지고있는 것이있다.Visual Studio 텍스트 상자의 데이터를 데이터베이스로 가져 오려면 어떻게합니까?

**public class DBConnectivity 
{ 
    private static DbConnection GetConnection() 
    { 
     string connString; 
     // change to your connection string in the following line 
     connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=I:\Applications Coursework\quiz.accdb"; 
     return new DbConnection(connString); 
    } 

    //method that saves a user in the db 
    public static void SaveName(string a, string b, string c) 
    { 
     DbConnection myConnection = DBConnectivity(); 
     string myQuery = "INSERT INTO user(user_name, user_age, user_country) VALUES ('" + a + "' , " + b + ", " + c + ")"; 
     OleDbCommand myCommand = new DbCommand(myQuery, myConnection); 
     try 
     { 
      myConnection.Open(); 
      myCommand.ExecuteNonQuery(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Exception in DBHandler", ex); 
     } 
     finally 
     { 
      myConnection.Close(); 
     } 
    }** 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    string a = TxbName.Text; 
    string b = TxbAge.Text ; 
    string c = TxbCountry.Text; 
    DBConnectivity.cs (a, b, c); 

} 
} 
+0

에 대한 MSDN에 대한 링크입니다. – Steve

+0

변수 myQuery의 출력을 인쇄 할 수 있습니까 – DevelopmentIsMyPassion

+1

SaveName 함수를 호출하는 것을 볼 수 없습니다. – DevelopmentIsMyPassion

답변

0

시험해주세요.

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      String a = TxbName.Text; 
      String b = TxbAge.Text ; 
      String c = TxbCountry.Text; 

      String Constring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=I:\Applications Coursework\quiz.accdb"; 
      String cmdinsert = @"INSERT INTO user(user_name, user_age, user_country) VALUES (@a, @b, @c)"; 
      OleDbConnection con = new OleDbConnection (Constring); 
      OleDbDataAdapter da = new OleDbDataAdapter(); 
      OleDbCommand cmd = new OleDbCommand(cmdinsert, con); 


      try 
      { 
       da.InsertCommand.Parameters.Add("@a", OleDbType.VarChar, 15); 
       da.InsertCommand.Parameters["a"].Value = TxbName.Text; 
       da.InsertCommand.Parameters.Add("@b", OleDbType.VarChar, 15); 
       da.InsertCommand.Parameters["b"].Value = TxbAge.Text; 
       da.InsertCommand.Parameters.Add("@c", OleDbType.VarChar, 15); 
       da.InsertCommand.Parameters["c"].Value = TxbCountry.Text; 

       con.Open(); 
       da.InsertCommand.ExecuteNonQuery(); 
       MessageBox.Show("Worked"); 
       con.Close(); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 



     } 
    } 
} 

내가 당신이 얻을 않는 예외 알고 궁금 OLEDB를 삽입 명령 http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.insertcommand%28v=vs.110%29.aspx

관련 문제