简体   繁体   English

C#和SQL Server 2012参数化插入未执行

[英]C# & SQL Server 2012 parameterized insert not executing

When executing SqlCommand.ExecuteNonQuery , the local database does not populate, and contains no tables before or after the command. 当执行SqlCommand.ExecuteNonQuery ,本地数据库不会填充,并且在命令之前或之后均不包含表。 Is this a problem of the coding, or possibly the database set up? 这是编码的问题,还是数据库的建立? I can rows to the table manually, and then query and read from it, but not insert. 我可以手动向表中行,然后查询并从中读取,但不能插入。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace FillGeologyDB
{
    public partial class Form1 : Form
    {
        Rock rock;
        List<Rock> rocks;
        string connectionString;
        string commandStatement;

        public Form1()
        {
            InitializeComponent();
            rock = new Rock();
            rocks = new List<Rock>();
            connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\BitMaintenance.mdf;Integrated Security=True;Connect Timeout=30";
            commandStatement = "INSERT INTO Metamorphic (rock_Name, rock_Color, rock_Feature, rock_Mineral),"
            + "VALUES (@rock_Name, @rock_Color, @rock_Features, @rock_Mineral)";
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            StreamReader reader = new StreamReader("Metamorphic Rock.txt");
            while(reader.Peek() > -1)
            {
                rock.RockName = reader.ReadLine();
                rocks.Add(rock);   
            }
            reader.Close();

            int i = 0;
            reader = new StreamReader("Color.txt");
            while (reader.Peek() > -1)
            {
                rocks[i].RockColor = reader.ReadLine();
                i++;
            }
            reader.Close();

            i = 0;
            reader = new StreamReader("Features.txt");
            while (reader.Peek() > -1)
            {
                rocks[i].RockFeatures = reader.ReadLine();
                i++;
            }
            reader.Close();

            i = 0;
            reader = new StreamReader("Mineral.txt");
            while (reader.Peek() > -1)
            {
                rocks[i].RockMineral = reader.ReadLine();
                i++;
            }
            reader.Close();
        }

        public void LoadData()
        {

           using(SqlConnection connection = new SqlConnection(connectionString))

                foreach (Rock r in rocks)
                {
                    using(SqlCommand command = new SqlCommand(commandStatement, connection))
                    {   
                        command.Parameters.AddWithValue("@rock_Name", rock.RockName);
                        command.Parameters.AddWithValue("@rock_Color", rock.RockColor);
                        command.Parameters.AddWithValue("@rock_Features", rock.RockFeatures);
                        command.Parameters.AddWithValue("@rock_Mineral", rock.RockMineral);
                        try
                        {
                            connection.Open();
                            command.ExecuteNonQuery();
                        }
                        catch(SqlException ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            connection.Close();
                        }
                    }

                }
        }
    }
}
 commandStatement = "INSERT INTO Metamorphic (rock_Name, rock_Color, rock_Feature, rock_Mineral),"
            + "VALUES (@rock_Name, @rock_Color, @rock_Features, @rock_Mineral)";

You have , after before VALUES and also you don't have " " you should fix that. 您在VALUES之前有,也没有" " ,则应解决此问题。 Also you can use symbol @ to write string on more than one line: 您也可以使用符号@在多行中写字符串:

commandStatement = @"INSERT INTO 
                         Metamorphic (rock_Name, rock_Color, rock_Feature, rock_Mineral)
                     VALUES 
                         (@rock_Name, @rock_Color, @rock_Features, @rock_Mineral)";

PS: When you have question, add what is your problem. PS:当您有疑问时,请添加您的问题。 Not working is not a problem. 不工作不是问题。 The code return an exception which can be valuable to understand your problem. 该代码返回一个异常,这对于理解您的问题可能很有价值。 So you must always write the exception message in your question. 因此,您必须始终在问题中写异常消息。

I have created and run this locally, as mybirthname pointed out, it appears to have fixed the issue. 正如mybirthname所指出的那样,我已经在本地创建并运行此文件,它似乎已解决了该问题。

You may run this locally as an example: 您可以在本地运行此示例:

class Program
{
    static void Main(string[] args)
    {
        var connectionString = @"Your Connection string goes here";
        var commandStatement = "INSERT INTO Metamorphic (rock_Name, rock_Color, rock_Feature, rock_Mineral)"
        + "VALUES (@rock_Name, @rock_Color, @rock_Features, @rock_Mineral)";

        using (SqlConnection connection = new SqlConnection(connectionString))
        using (SqlCommand command = new SqlCommand(commandStatement, connection))
        {
            command.Parameters.AddWithValue("@rock_Name", "One");
            command.Parameters.AddWithValue("@rock_Color", "Two");
            command.Parameters.AddWithValue("@rock_Features", "Thre");
            command.Parameters.AddWithValue("@rock_Mineral", "Four");
            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }
        } 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM