C# 参数化查询需要c中未提供的参数#
我正试图为一个虚构的音乐商店构建一个应用程序,但我一直在C# 参数化查询需要c中未提供的参数#,c#,mysql,winforms,datagridview,parameters,C#,Mysql,Winforms,Datagridview,Parameters,我正试图为一个虚构的音乐商店构建一个应用程序,但我一直在@Voornaamklant和@Achternaamklant上得到这个: 参数化查询需要未提供的参数 我正在从我的dataGridView 2中检索数据,它从另一个dataGridView和一些文本框中获取其值。然后我想在这里做的是将dataGridView2中的所有内容存储到我的数据库表“Factuur”中 我的dataGridView2中有9行,所有9行都正确地进入了我的数据库。然而,我的表中有10行,因为我的第一列是一个ID自动递增
@Voornaamklant
和@Achternaamklant
上得到这个:
参数化查询需要未提供的参数
我正在从我的dataGridView 2
中检索数据,它从另一个dataGridView
和一些文本框中获取其值。然后我想在这里做的是将dataGridView2
中的所有内容存储到我的数据库表“Factuur”中
我的dataGridView2
中有9行,所有9行都正确地进入了我的数据库。然而,我的表中有10行,因为我的第一列是一个ID自动递增的字段,也许这是什么?但我真的不认为这是原因
这是我的密码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NieuwefactuurV2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.dateTimePicker1.Value = DateTime.Now;
}
private void afsluitenToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
DataGridViewRow dr = dataGridView1.SelectedRows[0];
Titel.Text = dr.Cells["titelDataGridViewTextBoxColumn"].Value.ToString();
Prijs.Text = dr.Cells["prijsDataGridViewTextBoxColumn"].Value.ToString();
}
private void Aantal_TextChanged(object sender, EventArgs e)
{
try
{
double a = Convert.ToDouble(Aantal.Text);
double b = Convert.ToDouble(Prijs.Text);
Subtotaal.Text = (a * b).ToString();
}
catch
{
}
}
private void Prijs_TextChanged(object sender, EventArgs e)
{
try
{
double a = Convert.ToDouble(Aantal.Text);
double b = Convert.ToDouble(Prijs.Text);
double c = Convert.ToDouble(Subtotaal.Text);
double d = Convert.ToDouble(Korting.Text);
Subtotaal.Text = (a * b).ToString();
}
catch
{
}
}
private void Subtotaal_TextChanged(object sender, EventArgs e)
{
try
{
double a = Convert.ToDouble(Aantal.Text);
double b = Convert.ToDouble(Prijs.Text);
double c = Convert.ToDouble(Subtotaal.Text);
double d = Convert.ToDouble(Korting.Text);
if (d >= 1 && d < 100)
{
Totaal.Text = (c - (c / 100 * d)).ToString();
}
if (d <= 0)
{
Totaal.Text = (c).ToString();
}
if (d >= 100)
{
Totaal.Text = (0).ToString();
}
}
catch
{
}
}
private void Korting_TextChanged(object sender, EventArgs e)
{
try
{
double a = Convert.ToDouble(Aantal.Text);
double b = Convert.ToDouble(Prijs.Text);
double c = Convert.ToDouble(Subtotaal.Text);
double d = Convert.ToDouble(Korting.Text);
if (d >= 1 && d < 100)
{
Totaal.Text = (c - (c / 100 * d)).ToString();
}
if (d <= 0)
{
Totaal.Text = (c).ToString();
}
if (d >= 100)
{
Totaal.Text = (0).ToString();
}
}
catch
{
}
}
private void Add_Click(object sender, EventArgs e)
{
try
{
string str;
string titel = Titel.Text;
string voorupdate;
str = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[4].Value.ToString();
int a = Convert.ToInt32(str);
int b = Convert.ToInt32(Aantal.Text);
voorupdate = (a - b).ToString();
if (a - b >= 0)
{
Print.Enabled = true;
string vnaam = Voornaam.Text;
string anaam = Achternaam.Text;
string lpcd = LPCD.Text;
string dateColumn = dateTimePicker1.Text;
string firstColumn = Titel.Text;
string secondColumn = Aantal.Text;
string thirdColumn = Prijs.Text;
string fourthColumn = Subtotaal.Text;
string fifthColumn = Korting.Text;
string sixthColumn = Totaal.Text;
string[] row = { vnaam, anaam, firstColumn, secondColumn, thirdColumn, fourthColumn, fifthColumn, sixthColumn, lpcd, dateColumn };
dataGridView2.Rows.Add(row);
string cmd = "Update Album set [Actuele Voorraad]='" + voorupdate + "' where Titel='" + Titel.Text + "'";
using (SqlConnection connection = new SqlConnection("Data Source=Evan-PC;Initial Catalog=DatabaseProject;Integrated Security=True"))
{
using (SqlCommand command1 = new SqlCommand(cmd, connection))
{
connection.Open();
command1.ExecuteNonQuery();
}
}
}
else
{
Print.Enabled = false;
MessageBox.Show("Fout");
}
}
catch
{
MessageBox.Show("Vul waarden in");
}
}
private void Remove_Click(object sender, EventArgs e)
{
string str;
string str2;
string titel;
string voorupdate;
titel = dataGridView2.Rows[dataGridView2.SelectedRows[0].Index].Cells[2].Value.ToString();
str2 = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[5].Value.ToString();
str = dataGridView2.Rows[dataGridView2.SelectedRows[0].Index].Cells[3].Value.ToString();
int a = Convert.ToInt32(str2);
int b = Convert.ToInt32(str);
voorupdate = ((a + b) - b).ToString();
string cmd = "Update Album set [Actuele Voorraad]='" + voorupdate + "' where Titel='" + titel + "'";
using (SqlConnection connection = new SqlConnection("Data Source=Evan-PC;Initial Catalog=DatabaseProject;Integrated Security=True"))
{
using (SqlCommand command1 = new SqlCommand(cmd, connection))
{
connection.Open();
command1.ExecuteNonQuery();
}
}
if (this.dataGridView2.SelectedRows.Count > 0)
{
dataGridView2.Rows.RemoveAt(this.dataGridView2.SelectedRows[0].Index);
}
else
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'databaseProjectDataSet.Album' table. You can move, or remove it, as needed.
this.albumTableAdapter1.Fill(this.databaseProjectDataSet.Album);
// TODO: This line of code loads data into the 'database_ProjectDataSet2.Album' table. You can move, or remove it, as needed.
this.albumTableAdapter.Fill(this.database_ProjectDataSet2.Album);
}
private void Print_Click(object sender, EventArgs e)
{
try
{
string mail = Email.Text;
string voor = Voornaam.Text;
string achter = Achternaam.Text;
string adres = Adres.Text;
string woon = Woonplaats.Text;
string post = Postcode.Text;
string tele = Telefoonnummer.Text;
try
{
string addcity = "If Not Exists(select * from Woonplaats where Woonplaats='@woon') Begin insert into Woonplaats (Woonplaats)" + "Values('" + woon + "')END";
string dataquery = "INSERT INTO Klant([E-mail], Voornaam, Achternaam, Adres, Woonplaats, Postcode, Telefoonnummer) " +
"Values('" + mail + "', '" + voor + "', '" + achter + "', '" + adres + "', '" + woon + "', '" + post + "', '" + tele + "')";
using (SqlConnection connection = new SqlConnection("Data Source=Evan-PC;Initial Catalog=DatabaseProject;Integrated Security=True"))
{
using (SqlCommand command1 = new SqlCommand(addcity, connection))
using (SqlCommand command = new SqlCommand(dataquery, connection))
{
connection.Open();
command1.Parameters.AddWithValue("@woon", woon);
command1.ExecuteNonQuery();
command.ExecuteNonQuery();
command.Parameters.Clear();
command1.Parameters.Clear();
}
}
}
catch
{
string dataquery = "INSERT INTO Klant([E-mail], Voornaam, Achternaam, Adres, Woonplaats, Postcode, Telefoonnummer) " +
"Values('" + mail + "', '" + voor + "', '" + achter + "', '" + adres + "', '" + woon + "', '" + post + "', '" + tele + "')";
using (SqlConnection connection = new SqlConnection("Data Source=Evan-PC;Initial Catalog=DatabaseProject;Integrated Security=True"))
{
using (SqlCommand command = new SqlCommand(dataquery, connection))
{
connection.Open();
command.ExecuteNonQuery();
command.Parameters.Clear();
}
}
}
}
catch
{
MessageBox.Show("Klantgegevens niet goed ingevuld");
}
try
{
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
string dataquery = "INSERT INTO Factuur VALUES(@Voornaamklant, @Achternaamklant, @Albumtitel, @Aantal, @Prijs, @Subtotaal, @Korting, @Totaal, @CDofLP, @Datuminvoeren)";
using (SqlConnection connection = new SqlConnection("Data Source=Evan-PC;Initial Catalog=DatabaseProject;Integrated Security=True"))
{
using (SqlCommand command2 = new SqlCommand(dataquery, connection))
{
connection.Open();
command2.Parameters.AddWithValue("@Voornaamklant", dataGridView2.Rows[i].Cells["vnaam"].Value);
command2.Parameters.AddWithValue("@Achternaamklant", dataGridView2.Rows[i].Cells["anaam"].Value);
command2.Parameters.AddWithValue("@Albumtitel", dataGridView2.Rows[i].Cells["Albumtitels"].Value);
command2.Parameters.AddWithValue("@Aantal", dataGridView2.Rows[i].Cells["Aantal2"].Value);
command2.Parameters.AddWithValue("@Prijs", dataGridView2.Rows[i].Cells["Prijs2"].Value);
command2.Parameters.AddWithValue("@Subtotaal", dataGridView2.Rows[i].Cells["Subtotaal2"].Value);
command2.Parameters.AddWithValue("@Korting", dataGridView2.Rows[i].Cells["Korting2"].Value);
command2.Parameters.AddWithValue("@Totaal", Convert.ToDecimal((dataGridView2.Rows[i].Cells["Totaal2"].Value)));
command2.Parameters.AddWithValue("@CDofLP", dataGridView2.Rows[i].Cells["CDLP"].Value);
command2.Parameters.AddWithValue("@Datuminvoeren", Convert.ToDateTime(dateTimePicker1.Text));
command2.ExecuteNonQuery();
command2.Parameters.Clear();
}
}
}
}
catch
{
MessageBox.Show("Kan de factuur niet in de database zetten", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用System.Data.SqlClient;
使用系统图;
利用制度全球化;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间nieuwefacturv2
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
this.dateTimePicker1.Value=DateTime.Now;
}
私有void afsluitenToolStripMenuItem\u单击(对象发送方,事件参数e)
{
Close();
}
private void dataGridView1\u SelectionChanged(对象发送方,事件参数e)
{
DataGridViewRow dr=dataGridView1.SelectedRows[0];
Titel.Text=dr.Cells[“titelDataGridViewTextBoxColumn”].Value.ToString();
Prijs.Text=dr.Cells[“prijsDataGridViewTextBoxColumn”].Value.ToString();
}
私有void Aantal_TextChanged(对象发送方,事件参数e)
{
尝试
{
双a=Convert.ToDouble(Aantal.Text);
double b=Convert.ToDouble(Prijs.Text);
Subtotaal.Text=(a*b.ToString();
}
抓住
{
}
}
私有void Prijs_TextChanged(对象发送方,事件参数e)
{
尝试
{
双a=Convert.ToDouble(Aantal.Text);
double b=Convert.ToDouble(Prijs.Text);
双c=Convert.ToDouble(subtotal.Text);
双d=Convert.ToDouble(Korting.Text);
Subtotaal.Text=(a*b.ToString();
}
抓住
{
}
}
私有void Subtotaal_TextChanged(对象发送方,事件参数e)
{
尝试
{
双a=Convert.ToDouble(Aantal.Text);
double b=Convert.ToDouble(Prijs.Text);
双c=Convert.ToDouble(subtotal.Text);
双d=Convert.ToDouble(Korting.Text);
如果(d>=1&&d<100)
{
Totaal.Text=(c-(c/100*d)).ToString();
}
如果(d=100)
{
Totaal.Text=(0.ToString();
}
}
抓住
{
}
}
私有void Korting_TextChanged(对象发送方,事件参数e)
{
尝试
{
双a=Convert.ToDouble(Aantal.Text);
double b=Convert.ToDouble(Prijs.Text);
双c=Convert.ToDouble(subtotal.Text);
双d=Convert.ToDouble(Korting.Text);
如果(d>=1&&d<100)
{
Totaal.Text=(c-(c/100*d)).ToString();
}
如果(d=100)
{
Totaal.Text=(0.ToString();
}
}
抓住
{
}
}
私有void Add_Click(对象发送方,事件参数e)
{
尝试
{
字符串str;
字符串titel=titel.Text;
字符串更新;
str=dataGridView1.Rows[dataGridView1.SelectedRows[0]。Index]。单元格[4]。Value.ToString();
int a=转换为32(str);
int b=转换为32(Aantal.Text);
voorupdate=(a-b).ToString();
如果(a-b>=0)
{
Print.Enabled=true;
字符串vnaam=Voornaam.Text;
字符串anaam=Achternaam.Text;
字符串lpcd=lpcd.Text;
字符串dateColumn=dateTimePicker1.Text;
string firstColumn=Titel.Text;
字符串secondColumn=Aantal.Text;
字符串thirdColumn=Prijs.Text;
string fourthColumn=Subtotaal.Text;
字符串第五列=Korting.Text;
字符串第六列=Totaal.Text;
string[]row={vnaam,anaam,firstColumn,secondColumn,thirdColumn,fourthColumn,fifthColumn,sixthColumn,lpcd,dateColumn};
dataGridView2.Rows.Add(row);
string cmd=“更新相册集[Actuele Voorraad]=”“+voorupdate+”,其中Titel=”“+Titel.Text+”;
使用(SqlConnection连接=新的SqlConnection(“数据源=Evan PC;初始目录=DatabaseProject;集成安全性=True”))
{
使用(SqlCommand command1=newsqlcommand(cmd,connection))
{
connection.Open();
command1.ExecuteNonQuery();
}
}
}
其他的
{
Print.Enabled=false;
MessageBox.Show(“Fout”);
}
string dataquery = "INSERT INTO Factuur VALUES(@Voornaamklant, @Achternaamklant, @Albumtitel, @Aantal, @Prijs, @Subtotaal, @Korting, @Totaal, @CDofLP, @Datuminvoeren)";
MessageBox.Show("Kan de factuur niet in de database zetten", "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error);
if(dataGridView2.Rows[i].Cells["vnaam"].Value == null)
command2.Parameters.AddWithValue("@Voornaamklant", DBNull.Value);
else
command2.Parameters.AddWithValue("@Voornaamklant", dataGridView2.Rows[i].Cells["vnaam"].Value);