[英]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.