[英]C# & SQL Server 2012 parameterized insert not executing
当执行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();
}
}
}
}
}
}
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:当您有疑问时,请添加您的问题。 不工作不是问题。 该代码返回一个异常,这对于理解您的问题可能很有价值。 因此,您必须始终在问题中写异常消息。
正如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();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.