2012-08-29 106 views
0

我有班級報告和類程序,我想完成(迄今爲止沒有運氣)是從類程序和方法SaveRep()發送數據到類Report方法Save()並保存它在這個方法。保存對象到數據庫c#

我很抱歉,如果問題是嚴重製定,我真的堅持在這一點,請幫助。由於

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Configuration; 
using System.Data.SqlClient; 

namespace Application 
{ 
class Program 
{ 
    static void Main(string[] args) 
    {    
     //call method SaveRep 
    } 

    public void SaveRep(...) 
    { 
     int RepID = 1; 
     string Data1 = "SomeData1" 
     string Data2 = "SomeData2" 

     //This method should send the data above to method Save() in Report class 
     //data to this method will be provided from another method. 
    }   
    } 

    public class Report 
    { 
    private static int _repID; 
    public int RepID 
    { 
     get { return _repID; } 
     set { _repID = value; } 
    } 

    private static string _data1;  
    public string Data1 
    { 
     get { return _data1; } 
     set { _data1 = value; } 
    } 

    private static string __data2; 
    public string Data1 
    { 
     get { return _data2; } 
     set { _data2 = value; } 
    } 

    public void Save() 
    { 
     string strConnectionString = (@"server=(local)\sqlexpress;Integrated Security=True;database=DataBase"); 

     SqlConnection connection = new SqlConnection(strConnectionString); 
     connection.Open(); 

     // This method should save all data (RepID,Data1,Data2) 
     // to the DB, provided to her by SaveRep method from Program class. 
     // Properties are columns from a database 
    } 
    } 
} 
+1

恐怕這裏沒有問題。嘗試取得一些進展,並回來更具體的問題。 –

+0

我的問題是如何發送數據對象從SaveRep()保存()並將其保存到db.If有人有任何示例,將非常有幫助 – DareDevil

回答

0
public void Save() 
{ 
    string yourConnString="Replace with Your Database ConnectionString"; 
    using(SqlConnection connection = new SqlConnection(yourConnString)) 
    {  
    string sqlStatement = "INSERT Table1(Data1) VALUES(@Data1)"; 
    using(SqlCommand cmd = new SqlCommand(sqlStatement, connection)) 
    { 
     cmd.CommandType = CommandType.Text; 
     cmd.Parameters.AddWithValue("@Data1",Data1); 
     //add more parameters as needed and update insert query 

     try 
     { 
      connection.Open(); 
      cmd.ExecuteNonQuery(); 
     } 
     catch(Exception ex) 
     { 
      //log error 
     }  
    } 
    } 
} 

假設你Data1是您的列名的名稱。更新sqlStatement以獲得您的相關列。

現在SaveRep,你可以這樣調用它

public void SaveRep() 
{ 
    Report objReport=new Report(); 
    objReport.Data1="Something somethinng"; 
    objReport.Save(); 
} 
+0

好吧,我得到這部分保存()方法,但如何提供數據從SaveRep()方法到她? – DareDevil

+0

@DareDevil:我加了那個答案 – Shyju

+0

哦,我沒有看到它。謝謝;), – DareDevil

0
string connectionString = ....; //adjust your connection string 

    string queryString = "INSERT INTO ....;"; //Adjust your query 
    using (SqlConnection connection = new SqlConnection(
       connectionString)) 
    { 
     using(SqlCommand command = new SqlCommand(
      queryString, connection)) 
     { 
     connection.Open(); 
     command .ExecuteNonQuery(); 
     } 
    } 
0

一件小事情,以前的發言者都沒有注意到,那是你Report類是嚴重破損。爲什麼在變量上使用static關鍵字?不要那樣做!那麼,除非你知道它做了什麼,在這裏我有你沒有的感覺。更新您的報告類是這樣的:在原始代碼

public class Report 
{ 
    public int RepID {get;set;} 
    public string Data1 {get;set;} 
    public string Data2 {get;set;} 

    public void Save() 
    { 
     // what others said 
    } 
} 

「靜態」使所有TE變量的全局/共享。這意味着如果您創建的報表對象數爲new,則它們都具有相同的ID,Data1和Data2。對這500個對象中的任何一個進行任何更改都會導致所有對象發生更改。但這是誤導,對象不會改變。他們只會有相同的數據。看着「身份證」字段,我想你寧願要一個單獨的對象與單獨的數據分開記錄..