保存对象到数据库c# [英] saving object to database c#

查看:437
本文介绍了保存对象到数据库c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having class Report and class Program, what I want to accomplish (no luck so far) is to send data from class Program and method SaveRep() to class Report method Save() and save it in this method.

如果问题配方不当,我深表歉意,我真的很抱歉,请帮忙。感谢

I apologize if the question is badly formulated, I am really stuck at this, please help. Thanks

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


推荐答案

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以包含相关列。

Assuming your Data1 is the name of your Column name. Update the sqlStatement to have your relevant columns.

现在在 SaveRep ,可以像

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

这篇关于保存对象到数据库c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆