C#& SQL Server 2012参数化插入不执行

时间:2015-08-13 19:19:47

标签: c# sql-server

执行SqlCommand.ExecuteNonQuery时,本地数据库不会填充,并且在命令之前或之后不包含任何表。这是编码问题,还是数据库设置?我可以手动向表中行,然后查询并从中读取,但不能插入。

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();
                        }
                    }

                }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

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

,之前你有VALUES,而且你还没有" "你应该解决这个问题。您还可以使用符号@在多行上写字符串:

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

PS:如果您有疑问,请添加您的问题。不工作不是问题。代码返回一个异常,这对于理解您的问题很有价值。因此,您必须始终在问题中写下异常消息。

答案 1 :(得分:0)

我已在本地创建并运行此功能,正如mybirthname指出的那样,它似乎解决了这个问题。

您可以在本地运行此示例:

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();
            }
        } 
    }
}