当前位置:编程学习 > C#/ASP.NET >>

C#通讯录问题

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 通讯录
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("通讯录中的记录为: ");
            SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=txl;Integrated Security=True");
            sqlcon.Open();
            SqlCommand sqlCommand = sqlcon.CreateCommand();
            sqlCommand.CommandText = "select * from txl_1";
            SqlDataReader sqlReader = sqlCommand.ExecuteReader();
            while (sqlReader.Read())
            {
                Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", sqlReader["name"], sqlReader["mobilephone"], sqlReader["易做图"], sqlReader["age"]);
            }

            Console.WriteLine("请选择;1.增加记录 2.删除记录 3.查询 4.退出");

            string flag = Console.ReadLine();
            int n = int.Parse(flag);
            switch (n)
            {
                case 1:
                    //int k = 1;
                    //Console.WriteLine("按0停止输入");
                    //int s=int.Parse(Console.ReadLine());
                    //if (s != 0)
                    //    while (k == 1)
                    //    {
                    Console.WriteLine("请输入姓名");
                    string na = Console.ReadLine();
                    Console.WriteLine("请输入电话号码");
                    string ph = Console.ReadLine();
                    Console.WriteLine("请输入性别");
                    string se = Console.ReadLine();
                    Console.WriteLine("请输入年龄");
                    string ag = Console.ReadLine();
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select * from login", sqlcon);
                    SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlDataAdapter);
                    DataSet sqldateSet = new DataSet();
                    sqlDataAdapter.Fill(sqldateSet, "txl_1");
                    DataRow sqlRow = sqldateSet.Tables["txl_1"].NewRow();
                    sqlRow["name"] = na;
                    sqlRow["mobilephone"] = ph;
                    sqlRow["易做图"] = se;
                    sqlRow["age"] = ag;

                    //Console.WriteLine("增加后的通讯录为: ");
                    sqlDataAdapter.Update(sqldateSet, "txl_1");
                    // }

                    //Console.WriteLine();
                    sqlCommand.Connection.Close();
                    break;

                case 2:

                    Console.WriteLine("请输入要删除的姓名");
                    string nam = Console.ReadLine();
                    SqlConnection sqlco = new SqlConnection("Data Source=.;Initial Catalog=txl;Integrated Security=True");

                    SqlCommand sqlComm = sqlcon.CreateCommand();
                    SqlDataAdapter sqlDataAdapte = new SqlDataAdapter("select name from txl_1", sqlco);
                    DataSet sqldateSet1 = new DataSet();

                    sqlDataAdapte.Fill(sqldateSet1, "txl_1");
                    DataColumn[] keys = new DataColumn[1];
                    keys[0] = sqldateSet1.Tables["txl_1"].Columns["name"];


                    DataRow findRow = sqldateSet1.Tables["txl_1"].Rows.Find(nam);
                    if (findRow == null)
                    {
                        sqlDataAdapte.Update(sqldateSet1, "txl_1");
                    }
                    Console.WriteLine("删除完成");
                    break;

                case 3:
                    Console.WriteLine("更新后的通讯录中的记录为: ");
                    SqlConnection sqlc = new SqlConnection("Data Source=.;Initial Catalog=txl;Integrated Security=True");
                    sqlcon.Open();
                    SqlCommand sqlComman = sqlcon.CreateCommand();
                    sqlCommand.CommandText = "select * from txl_1";
                    SqlDataReader sqlReade = sqlCommand.ExecuteReader();
                    while (sqlReade.Read())
                    {
                        Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", sqlReade["name"], sqlReade["mobilephone"], sqlReade["易做图"], sqlReade["age"]);
                    }
                    sqlComman.Connection.Close();
                    break;

                case 4:

                    Console.WriteLine("xx");
                    break;



            }

        }
    }
}
以上是代码,运行可以,但一执行查询和删除的命令就会报错,请问哪里有问题?
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,