发送数据访问数据库

Posted

技术标签:

【中文标题】发送数据访问数据库【英文标题】:Sending data to acess database 【发布时间】:2017-01-31 20:08:28 【问题描述】:

我正在尝试将数据发送到访问数据库,但它一直给出相同的错误(ExecuteNonQuery:连接属性尚未初始化),我试图找到相关主题的答案,但我似乎看不出问题出在哪里,请帮忙,谢谢。代码如下:

    public static string[] questions = new string[7];

    private void SaveData_Load(object sender, EventArgs e)
    
        //Selecting data from login
        string fileName = @"U:\Projects\Visual Studio\Extras resources\login.xlsx";
        string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=0;Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName);

        using (OleDbConnection cn = new OleDbConnection(connectionString))
        
            try
            
                cn.Open();
                OleDbCommand command = new OleDbCommand("select * from [lineAudit$]", cn);
                using (OleDbDataReader dr = command.ExecuteReader())
                
                    while (dr.Read())
                    
                        string row1Col0 = Convert.ToString(dr[0]);
                        string row1cl1 = Convert.ToString(dr[1]);
                        dateTime_bx.Text = row1Col0;
                        auditor_bx.Text = row1cl1;
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show("Failed due to" + ex.Message);
            
            cn.Close();
        

        //Selecting data from datawindow
        string fileName2 = @"U:\Projects\Visual Studio\Extras resources\dataWindow.xlsx";
        string connectionString2 = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=0;Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName2);

        using (OleDbConnection cn = new OleDbConnection(connectionString2))
        
            try
            
                cn.Open();
                OleDbCommand command = new OleDbCommand("select * from [lineAudit$]", cn);
                using (OleDbDataReader dr = command.ExecuteReader())
                
                    while (dr.Read())
                    
                        string row1Col0 = Convert.ToString(dr[0]);
                        string row1Col1 = Convert.ToString(dr[1]);
                        string row1Col2 = Convert.ToString(dr[2]);
                        string row1Col3 = Convert.ToString(dr[3]);
                        opId_bx.Text = row1Col0;
                        shopOrder_bx.Text = row1Col1;
                        partNumber_bx.Text = row1Col2;
                        opNumber_bx.Text = row1Col3;
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show("Failed due to" + ex.Message);
            
            cn.Close();
        

        //Selecting data from documentation
        string fileName3 = @"U:\Projects\Visual Studio\Extras resources\documentation.xlsx";
        string connectionString3 = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=0;Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName3);

        using (OleDbConnection cn = new OleDbConnection(connectionString3))
        
            try
            
                cn.Open();
                OleDbCommand command = new OleDbCommand("select * from [lineAudit$]", cn);
                using (OleDbDataReader dr = command.ExecuteReader())
                
                    while (dr.Read())
                    
                        string row1Col0 = Convert.ToString(dr[0]);
                        string row1Col1 = Convert.ToString(dr[1]);
                        q1_bx.Text = row1Col0;

                        //boolean to check if string is empty
                        Documentation doc = new Documentation();
                        if (a == 2)
                        
                            questions[0] = " Q1- " + row1Col1;

                        
                        else
                        
                        
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show("Failed due to" + ex.Message);
            
            cn.Close();
        

        //Selecting data from materials
        string fileName4 = @"U:\Projects\Visual Studio\Extras resources\materials.xlsx";
        string connectionString4 = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=0;Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName4);

        using (OleDbConnection cn = new OleDbConnection(connectionString4))
        
            try
            
                cn.Open();
                OleDbCommand command = new OleDbCommand("select * from [lineAudit$]", cn);
                using (OleDbDataReader dr = command.ExecuteReader())
                
                    while (dr.Read())
                    
                        string row1Col0 = Convert.ToString(dr[0]);
                        string row1Col1 = Convert.ToString(dr[1]);
                        q2_bx.Text = row1Col0;

                        //boolean to check if string is empty
                        if (b == 2)
                        
                            questions[1] = " Q2- " + row1Col1;
                        
                        else
                        
                        
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show("Failed due to" + ex.Message);
            
            cn.Close();
        

        //Selecting data from procedure
        string fileName5 = @"U:\Projects\Visual Studio\Extras resources\comformity.xlsx";
        string connectionString5 = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=0;Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName5);

        using (OleDbConnection cn = new OleDbConnection(connectionString5))
        
            try
            
                cn.Open();
                OleDbCommand command = new OleDbCommand("select * from [lineAudit$]", cn);
                using (OleDbDataReader dr = command.ExecuteReader())
                
                    while (dr.Read())
                    
                        string row1Col0 = Convert.ToString(dr[0]);
                        string row1Col1 = Convert.ToString(dr[1]);
                        q3_bx.Text = row1Col0;

                        //boolean to check if string is empty
                        if (c == 2)
                        
                            questions[2] = " Q3- " + row1Col1;
                        
                        else
                        
                        
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show("Failed due to" + ex.Message);
            
            cn.Close();
        

        //Selecting data from No_procedure
        string fileName6 = @"U:\Projects\Visual Studio\Extras resources\proceadure.xlsx";
        string connectionString6 = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=0;Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName6);

        using (OleDbConnection cn = new OleDbConnection(connectionString6))
        
            try
            
                cn.Open();
                OleDbCommand command = new OleDbCommand("select * from [lineAudit$]", cn);
                using (OleDbDataReader dr = command.ExecuteReader())
                
                    while (dr.Read())
                    
                        string row1Col0 = Convert.ToString(dr[0]);
                        string row1Col1 = Convert.ToString(dr[1]);
                        q4_bx.Text = row1Col0;

                        //boolean to check if string is empty
                        if (d == 2)
                        
                            questions[3] = " Q4- " + row1Col1;
                        
                        else
                        
                        
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show("Failed due to" + ex.Message);
            
            cn.Close();
        

        //Selecting data from calibration
        string fileName7 = @"U:\Projects\Visual Studio\Extras resources\calibration.xlsx";
        string connectionString7 = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=0;Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName7);

        using (OleDbConnection cn = new OleDbConnection(connectionString5))
        
            try
            
                cn.Open();
                OleDbCommand command = new OleDbCommand("select * from [lineAudit$]", cn);
                using (OleDbDataReader dr = command.ExecuteReader())
                
                    while (dr.Read())
                    
                        string row1Col0 = Convert.ToString(dr[0]);
                        string row1Col1 = Convert.ToString(dr[1]);
                        q5_bx.Text = row1Col0;

                        //boolean to check if string is empty
                        if (f == 2)
                        
                            questions[4] = " Q5- " + row1Col1;
                        
                        else
                        
                        
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show("Failed due to" + ex.Message);
            
            cn.Close();
        

        //Selecting data from safety
        string fileName8 = @"U:\Projects\Visual Studio\Extras resources\safety.xlsx";
        string connectionString8 = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=0;Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName8);

        using (OleDbConnection cn = new OleDbConnection(connectionString8))
        
            try
            
                cn.Open();
                OleDbCommand command = new OleDbCommand("select * from [lineAudit$]", cn);
                using (OleDbDataReader dr = command.ExecuteReader())
                
                    while (dr.Read())
                    
                        string row1Col0 = Convert.ToString(dr[0]);
                        string row1Col1 = Convert.ToString(dr[1]);
                        q6_bx.Text = row1Col0;

                        //boolean to check if string is empty
                        if (g == 2)
                        
                            questions[5] = " Q6- " + row1Col1;
                        
                        else
                        
                        
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show("Failed due to" + ex.Message);
            
            cn.Close();
        

        //Selecting data from packing
        string fileName9 = @"U:\Projects\Visual Studio\Extras resources\packing.xlsx";
        string connectionString9 = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=0;Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName9);

        using (OleDbConnection cn = new OleDbConnection(connectionString9))
        
            try
            
                cn.Open();
                OleDbCommand command = new OleDbCommand("select * from [lineAudit$]", cn);
                using (OleDbDataReader dr = command.ExecuteReader())
                
                    while (dr.Read())
                    
                        string row1Col0 = Convert.ToString(dr[0]);
                        string row1Col1 = Convert.ToString(dr[1]);
                        q7_bx.Text = row1Col0;

                        //boolean to check if string is empty
                        if (h == 2)
                        
                            questions[6] = " Q7- " + row1Col1;

                        
                        else
                        
                        
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show("Failed due to" + ex.Message);
            
            cn.Close();
        

        // Call the methods.
        string result1 = ConvertStringArrayToString(questions);
        noteStr_bx.Text = result1;
    

    string ConvertStringArrayToString(string[] questions)
    

        // Concatenate all the elements into a StringBuilder.
        StringBuilder builder = new StringBuilder();
        foreach (string value in questions)
        
            builder.Append(value);
            builder.Append('.');
        
        return builder.ToString();
    
    private void BTNSave_Click(object sender, EventArgs e)
    
        //sending data to database
        OleDbConnection connect = new OleDbConnection();
        connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=U:\Projects\Visual Studio\Extras resources\line_Audits.accdb";
        string QueryText = "INSERT INTO lineAudit (dateTime,auditor,operator,partNumber,shopOrder,operationNumber,documentationAndRevision,labeling,conformity,procedure,calibration,safety,packing,Notes) values (@dateTime,@auditor,@operator,@partNumber,@shopOrder,@operationNumber,@documentationAndRevision,@labeling,@conformity,@procedure,@calibration,@safety,@packing,@Notes)";
        connect.Open();
        using (OleDbCommand command = new OleDbCommand(QueryText))
        
            try
            
                OleDbDataAdapter da = new OleDbDataAdapter("INSERT INTO lineAudit", connect);

                String dateTime = dateTime_bx.Text;
                String auditor = auditor_bx.Text;
                String operador = opId_bx.Text;
                String partNumber = partNumber_bx.Text;
                String shopOrder = shopOrder_bx.Text;
                String operationNumber = opNumber_bx.Text;
                String q1 = q1_bx.Text;
                String q2 = q2_bx.Text;
                String q3 = q3_bx.Text;
                String q4 = q4_bx.Text;
                String q5 = q5_bx.Text;
                String q6 = q6_bx.Text;
                String q7 = q7_bx.Text;
                String note = noteStr_bx.Text;

                command.Parameters.AddWithValue("@dateTime", this.dateTime_bx.Text);
                command.Parameters.AddWithValue("@auditor", this.auditor_bx.Text);
                command.Parameters.AddWithValue("@operator", this.opId_bx.Text);
                command.Parameters.AddWithValue("@partNumber", this.partNumber_bx.Text);
                command.Parameters.AddWithValue("@shopOrder", this.shopOrder_bx.Text);
                command.Parameters.AddWithValue("@operationNumber", this.opNumber_bx.Text);
                command.Parameters.AddWithValue("@documentationAndRevision", this.q1_bx.Text);
                command.Parameters.AddWithValue("@labeling", this.q2_bx.Text);
                command.Parameters.AddWithValue("@conformity", this.q3_bx.Text);
                command.Parameters.AddWithValue("@procedure", this.q4_bx.Text);
                command.Parameters.AddWithValue("@calibration", this.q5_bx.Text);
                command.Parameters.AddWithValue("@safety", this.q6_bx.Text);
                command.Parameters.AddWithValue("@packing", this.q7_bx.Text);
                command.Parameters.AddWithValue("@Notes", this.noteStr_bx.Text);

                command.ExecuteNonQuery();
                //connect.Close();
            
            catch (Exception ex)
            
                MessageBox.Show(ex.Message);
                connect.Close();
            
        

    

    private void dateTime_bx_TextChanged(object sender, EventArgs e)
    
    
    

【问题讨论】:

将数据发送到数据库的部分在代码的底部,它有一个注释行“//发送数据到数据库” 您需要将连接传递给OleDbCommand 的构造函数。顺便问一下,为什么你同时拥有OleDbCommandOleDbDataAdapter?我没有看到适配器用于任何用途。 如果有问题is at the bottom of the code ExecuteNonQuery: Connection property has not been initialized.的可能重复 @JohnWu 那么你认为我应该使用 OleDbCommand 而不是 OleDbDataAdapter 吗?我是 C# 新手,所以我不太确定何时使用其中一个。 【参考方案1】:

告诉您的 OleDbCommand 在 QueryText 之后使用什么连接 而不是:

using (OleDbCommand command = new OleDbCommand(QueryText))

使用:

using (OleDbCommand command = new OleDbCommand(QueryText, connect))

【讨论】:

感谢@Charles 的回答我可能不得不做一些其他的改变,但它奏效了! (我的主要问题是,当我创建数据库时,我选择了一些列作为整数,并通过代码将其作为字符串发送,我还必须将 [] 添加到我的插入字符串中的某些名称中。谢谢大家回复:D【参考方案2】:

这样的东西应该适合你。

using ExcelToAccessLib; 
using System; 
using System.IO; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
 
    public partial class Form1 : Form 
     
        private string AccessFile = Path.Combine( 
            AppDomain.CurrentDomain.BaseDirectory, "Database1.accdb"); 

        private string ExcelFile = Path.Combine( 
            AppDomain.CurrentDomain.BaseDirectory, "Customers.xlsx"); 

        public Form1() 
         
            InitializeComponent(); 
         

        private void button1_Click(object sender, EventArgs e) 
         
            Importer ImportData = 
                new Importer( 
                    new ExcelInfo 
                     
                        FileName = ExcelFile, 
                        HasHeaders = true, 
                        SheetName = "Customers" 
                    , 
                    new AccessInfo 
                     
                        FileName = AccessFile, 
                        TableName = "Customers1", 
                        FieldNames = "CompanyName,ContactName" 
                    ); 

            if (ImportData.Run()) 
             
                MessageBox.Show("Import complete"); 
             
         
     

【讨论】:

以上是关于发送数据访问数据库的主要内容,如果未能解决你的问题,请参考以下文章

Java基础知识—发送Emai和访问MySQL数据库

如何将数组数据从控制器发送到刀片并访问 javascript 变量中的数据

如何从 WebView Cordova AngularJS 控制器访问/发送数据到 MainActivity?

I2C 从机发送完数据后,接收不到主机(接收到倒数第二个数据后)发送的停止条件

如何从 .NET 用户界面将 MySQL 连接信息发送到数据访问层

如何从数据库获取/发送/提供/(使可访问)数据到静态 jquery 文件?从快速 EJS 到静态 Jquery?