我是个初学者正在做个人通信录 谁帮我看看我这个错在哪啊 我自己弄不懂 最好能帮我改下
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Contact
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void getInfo()
{
string connstr = "Data Source=LIAOZIJIAN-PC\\SQLEXPRESS;Initial Catalog=liaozijian;Integrated Security=True;Pooling=False";
string sql = "select Fid,Fname,Fphone,Faddress from friend";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
try
{
this.listView1.Items.Clear();
while (reader.Read())
{
string[] subItems = new string[]{
reader.GetInt32(0).ToString(),
reader.GetString(1),
reader.GetString(2),
reader.GetString(3)};
this.listView1.Items.Add(new ListViewItem(subItems));
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void addInfo(string name,string phone,string address)
{
string connstr = "Data Source=LIAOZIJIAN-PC\\SQLEXPRESS;Initial Catalog=liaozijian;Integrated Security=True;Pooling=False";
string sql="insert intofriend(Fname,Fphone,Faddress)values('"+name+"','"+phone+"','"+address+"')";
SqlConnection conn=new SqlConnection(connstr);
SqlCommand cmd=new SqlCommand(sql,conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
private void btnEdit_Click(object sender, EventArgs e)
{
string connstr = "Data Source=LIAOZIJIAN-PC\\SQLEXPRESS;Initial Catalog=liaozijian;Integrated Security=True;Pooling=False";
if (this.listView1.SelectedItems.Count == 0)
{
MessageBox.Show("请单击选择需要修改的联系人!");
return;
}
this.enableInfo();
ListViewItem lvi = this.listView1.SelectedItems[this.listView1.SelectedItems.Count - 1];
this.txtName.Text = lvi.SubItems[1].Text;
this.txtPhone.Text = lvi.SubItems[2].Text;
this.txtAddress.Text = lvi.SubItems[3].Text;
Fid = Convert.ToInt32(lvi.SubItems[0].Text);
this.isAdd = false;
this.btnDel.Enabled = false;
this.btnEdit.Enabled = false;
}
private void btnDel_Click(object sender, EventArgs e)
{
string connstr = "Data Source=LIAOZIJIAN-PC\\SQLEXPRESS;Initial Catalog=liaozijian;Integrated Security=True;Pooling=False";
int infoID = this.getselectid();
if (infoID == 0)
{
MessageBox.Show("请选中信息先!");
return;
}
try
{
string.sql = "delete friend where fid=" + infoID;
SqlConection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmd = new SqlCommand(Sql, conn);
cmd.ExecuteNonQucry();
conn.Close();
this.getInfo();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}