0

我有类 Report 和类 Program,我想要完成的(到目前为止还没有运气)是将数据从类 Program 和方法 SaveRep() 发送到类 Report 方法 Save() 并将其保存在此方法中。

如果问题表述不当,我深表歉意,我真的很困惑,请帮忙。谢谢

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
    }
  }
}
4

3 回答 3

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();
       }
    }
于 2012-08-29T20:58:06.927 回答
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();
 }
于 2012-08-29T21:05:28.673 回答
0

前几位演讲者没有注意到的一件小事,就是你的Report班级被严重破坏了。为什么static在变量上使用关键字?不要那样做!好吧,除非你知道它是做什么的,而在这里我感觉你不知道。将您的 Report 类更新为如下所示:

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 变量成为全局/共享的。这意味着如果您使用 来创建 500 个报表对象new,它们都将具有相同的 ID,即 Data1 和 Data2。对这 500 个对象中的任何一个进行任何更改都会导致所有对象发生更改。但它具有误导性,对象不会改变。他们只会拥有相同的数据。查看“ID”字段,我认为您宁愿想要一个具有单独数据的单独对象以用于单独的记录..

于 2012-08-30T12:09:24.043 回答