无法使用C#和ADO.NET添加到数据库

时间:2019-12-23 09:23:40

标签: c# ado.net

我想编写一个程序,将.csv中的所有数据保存到数据库中。

我使用基于服务的数据库。

但是,当我想像以前一样使用addtoTablesName并在大学学习时,突然出现了错误。

这是我的代码:

class Program
{
    static void Main(string[] args)
    {
        Database1Entities1 dbb = new Database1Entities1();//object database
        excel.Application xlApp;
        excel.Workbook xlWb;
        excel._Worksheet xlWs;
        excel.Range xlRange;

        excel.Application xlApp1;
        excel.Workbook xlWb1;
        excel._Worksheet xlWs1;
        excel.Range xlRange1;

        int rowFile = 0;
        int colFile = 0;
        string[][] dataFile;
        DateTime date = DateTime.Now;
        string tgl = date.Date.ToString("dd");
        string bln = date.Month.ToString("d2");
        string thn = date.Year.ToString();
        string tglskrg = thn+bln+tgl;
        string[] items;
        DateTime otime2 = DateTime.ParseExact(tglskrg, "yyyyMMdd", null);

        xlApp = new excel.Application();

        string[] filesindirectory = Directory.GetDirectories(@"C:\Users\u532246\Desktop\TRAVELOKA");

        foreach (string files in filesindirectory)
        {
            string name = Path.GetFileName(files);
            string subname = name.Substring(10, 8);
            DateTime subTime = DateTime.ParseExact(subname, "yyyyMMdd", null);
            TimeSpan span = otime2.Subtract(subTime);

            if(span.Days < 2)
            {
                // Console.WriteLine(files);
                foreach(string subfiles in Directory.GetFiles(files))
                {
                    xlWb = xlApp.Workbooks.Open(subfiles);
                    xlWs = xlWb.Sheets[1];
                    xlRange = xlWs.UsedRange;
                    rowFile = xlRange.Rows.Count;
                    colFile = xlRange.Columns.Count;

                    dataFile = new string[rowFile][];

                    for(int i = 0; i < dataFile.Length; i++)
                    {
                        dataFile[i] = new string[colFile];
                    }

                    for(int i = 0; i<rowFile;i++)
                    {
                        for(int j = 0; i<colFile;j++)
                        {
                            try
                            {
                                dataFile[i][j] = xlWs.Cells[i + 1, j + 1].value2.ToString();
                                //MessageBox.Show(dataMapping[i][j]);
                            }
                            catch (Exception ee)
                            {
                                dataFile[i][j] = "";
                            }
                        }
                    }
                    CMRTable cmr = new CMRTable();
                    for(int i = 0; i < rowFile; i++)
                    {
                        items = dataFile[i][0].Split(';');
                        cmr.Id = i + 1;
                        cmr.Merchant_Name = items[1];
                        cmr.Sett_Date = items[2];
                        cmr.Proc_Date = items[3];
                        cmr.Mid = items[4];
                        cmr.CardType = items[5];
                        cmr.Trx_Date = items[6];
                        cmr.Jam_Trx = items[7];
                        cmr.Auth = items[8];
                        cmr.CardNo = items[9];
                        cmr.Trx_Type = items[10];
                        cmr.Amount = items[11];
                        cmr.Rate = items[12];
                        cmr.Disc_Amt = items[13];
                        cmr.Tenor_Ins = items[14];
                        cmr.Rate_Ins = items[15];
                        cmr.Disc_Ins = items[16];
                        cmr.Net_Amt = items[17];
                        cmr.Purchase_Id = items[18];
                        cmr.Mechant_Descriptor = items[19];
                        dbb.AddToCMRTables(cmr); #I CANT USE THIS FUNCTION , DONT KNOW WHY 
                        dbb.SaveChanges();
                    }

                    Console.WriteLine(subfiles);
                } 
            }
        }
    }
}

有人可以帮助我吗?我想我已经像上大学时一样做了所有事情。

编辑:这是我得到的错误:

  

Database1Entities1不包含AddToCMRTables的定义,找不到可以接受的扩展方法AddToCMRTables接受类型为Database1Entities1的第一个参数

我复制了我以前在大学里做过的事情(这是代码)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{//dipakai ketika ingin menginsert data ke database.
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            displayData();

        }
        Database1Entities db = new Database1Entities();//object database
        private void displayData() 
        {
            var query = from x 
                           in db.MsStudents 
                        select x;
            dataGridView1.DataSource = query;
        }



        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtID.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
            txtName.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
        }//buat ngeklik yg ada di data grid view

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            search(txtSearch.Text);
        }
        public void search(string kata) {
            var query = from x
                        in db.MsStudents
                        where x.Name.Contains(kata)//apa yang muncul di kotak search itu di select
                        select x;
            dataGridView1.DataSource = query;
        }//function buat search

        private void btnInsert_Click(object sender, EventArgs e)
        {
            MsStudent student = new MsStudent();

            student.ID = Int32.Parse(txtID.Text);
            student.Name = txtName.Text;
            db.AddToMsStudents(student);
            db.SaveChanges();

            displayData();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            foreach (Control a in this.Controls)
            {
                if (a.GetType() == typeof(TextBox))
                {

                    a.Text = "";
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

0 个答案:

没有答案