C# p> 。。。循环时,将依次使用每个项目的文本。那么,您是如何创建参数的?你在别的地方用过!请不要在delete语句中使用*代替delete FROM。我在第二个字段中有项目,并且我根据第二个字段的名称删除。您所说的“第二个字段”是什么意思?您的错误是由于您
C# p> 。。。循环时,将依次使用每个项目的文本。那么,您是如何创建参数的?你在别的地方用过!请不要在delete语句中使用*代替delete FROM。我在第二个字段中有项目,并且我根据第二个字段的名称删除。您所说的“第二个字段”是什么意思?您的错误是由于您,c#,sql,database,sql-delete,C#,Sql,Database,Sql Delete,p> 。。。循环时,将依次使用每个项目的文本。那么,您是如何创建参数的?你在别的地方用过!请不要在delete语句中使用*代替delete FROM。我在第二个字段中有项目,并且我根据第二个字段的名称删除。您所说的“第二个字段”是什么意思?您的错误是由于您正在引用。SelectedItems[1]而您在列表中只选择了一个项目。列表中的选定项与字段无关。好的,我已修复,谢谢。但现在的问题是,它不会将其从数据库中删除。我没有得到一个错误,但它也不会从数据库中删除它。列表框中的文本是查询字段中的值吗?
p> 。。。循环时,将依次使用每个项目的文本。那么,您是如何创建参数的?你在别的地方用过!请不要在delete语句中使用*代替delete FROM。我在第二个字段中有项目,并且我根据第二个字段的名称删除。您所说的“第二个字段”是什么意思?您的错误是由于您正在引用
。SelectedItems[1]
而您在列表中只选择了一个项目。列表中的选定项与字段无关。好的,我已修复,谢谢。但现在的问题是,它不会将其从数据库中删除。我没有得到一个错误,但它也不会从数据库中删除它。列表框中的文本是查询
字段中的值吗?如果没有,这就是为什么它不删除…是的,我试图获取listview的第二个字段,但是我该怎么做呢?我认为它与listView1有关。选择editems[0]。Text
然后将其更改为:listView1。选择editems[1]。Text
以获取第二个字段值。但我不这么认为,你有什么建议吗?
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.SqlServerCe;
namespace Database_Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:\automail.sdf");
////////////////////////////////////Methodes////////////////////////////////////
private void Populate()
{
SqlCeCommand cm = new SqlCeCommand("SELECT * FROM Emails ORDER BY principalID", conn);
listView1.Items.Clear();
try
{
SqlCeDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
ListViewItem it = new ListViewItem(dr["principalID"].ToString());
it.SubItems.Add(dr["query"].ToString());
it.SubItems.Add(dr["email"].ToString());
it.SubItems.Add(dr["subject"].ToString());
listView1.Items.Add(it);
}
dr.Close();
dr.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Application.ExitThread();
}
}
////////////////////////////////////Methodes////////////////////////////////////
// Insert button (for data insert)
private void button1_Click(object sender, EventArgs e)
{
if ( txtEmail.Text == "" || txtQuery.Text == "")
{
MessageBox.Show("Fill in all the information first!");
}
else
{
SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Emails(principalID, email, query, subject) VALUES(@principalID, @email, @query, @subject)", conn);
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@principalID", txtPrincipal.Text);
cmd.Parameters.AddWithValue("@email", txtEmail.Text);
cmd.Parameters.AddWithValue("@query", txtQuery.Text);
cmd.Parameters.AddWithValue("@subject", txtSubject.Text);
try
{
int affectedrows = cmd.ExecuteNonQuery();
if (affectedrows > 0)
{
Populate();
MessageBox.Show("Email added successfully!");
txtEmail.Clear();
txtPrincipal.Clear();
txtQuery.Clear();
txtSearch.Clear();
txtSubject.Clear();
}
else
{
MessageBox.Show("Failed to add email!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
//if form shown fill the listview with the new information of the database
private void Form1_Shown(object sender, EventArgs e)
{
try
{
conn.Open();
Populate();
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.Message);
Application.ExitThread();
}
}
//open form 2 and hide this
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
Form2 form2 = new Form2();
form2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//if listview selected enable button, else disable
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
//enable delete button
button3.Enabled = true;
ListViewItem itm = listView1.SelectedItems[0];
string principalid = itm.SubItems[0].Text;
string query = itm.SubItems[1].Text;
string email = itm.SubItems[2].Text;
string subject = itm.SubItems[3].Text;
txtPrincipal.Text = principalid.ToString();
txtEmail.Text = email.ToString();
txtQuery.Text = query.ToString();
txtSubject.Text = subject.ToString();
}
else
{
button3.Enabled = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
}
//delete record button
private void button3_Click_1(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count <= 0)
{
MessageBox.Show("Select a record!");
}
else
{
ListView.SelectedListViewItemCollection items = this.listView1.SelectedItems;
//for (int i = 0; i < listView1.SelectedItems.Count; i++)
foreach(ListViewItem item in items)
{
SqlCeCommand cm = new SqlCeCommand("DELETE * FROM Emails WHERE query ='" + listView1.SelectedItems[1].Text + "'", conn);
try
{
cm.ExecuteNonQuery();
Populate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
// if button clicked, transfer information out of the records selected to form2
private void button2_Click_1(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you want to close this application?\nThis will terminate all systems which are active at the moment","Close", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
}
if (result == DialogResult.No)
{
e.Cancel = true;
}
}
private void button4_Click(object sender, EventArgs e)
{
//try
//{
// SqlCeCommand cm = new SqlCeCommand("SELECT * FROM Emails where principalID like " + txtSearch.Text, conn);
//}
//catch (Exception ex)
//
// MessageBox.Show(ex.Message);
//}
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
}
}
}
SqlCeCommand cm = new SqlCeCommand("DELETE FROM Emails WHERE query ='" + listView1.SelectedItems[0].Text + "'", conn);
listView1.SelectedItems[1].Text
listView1.SelectedItems[0].Text
listView1.SelectedItems[i].Text