0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace FileName
{
    class Database
    {
        public SqlConnection connection;
        public SqlCommand command;

        public string connString = @"Data Source=KAT;Initial Catalog=dbFileDirectory2;Integrated Security=True";

        private bool open()
        {
            try
            {
                connection = new SqlConnection(connString);
                connection.Open();
                return true;
            }
            catch (SqlException e)
            {
                return false;
            }
        }

        private void close()
        {
            try
            {
                connection.Close();
            }
            catch (SqlException e)
            {

            }
        }

        public bool testConnection()
        {
            if (open())
                return true;
            else
                return false;
        }


        public bool select(string query)
        {
            if (execute(query))
                return true;
            else
                return false;

        }

        public bool insert(string query)
        {
            if (execute(query))
                return true;
            else
                return false;
        }

        public bool update(string query)
        {
            if (execute(query))
                return true;
            else
                return false;
        }

        public bool delete(string query)
        {
            if (execute(query))
                return true;
            else
                return false;

        }

        public DataTable read(string query)
        {
            DataTable table;
            SqlDataAdapter adapter;
            try
            {
                open();
                adapter = new SqlDataAdapter(query, connection);
                table = new DataTable();
                adapter.Fill(table);
                return table;
            }
            catch (Exception e) 
            {
                MessageBox.Show(e.Message); return null; 
            }
            finally
            {
                close();
            }
        }

        private bool execute(string query)
        {
            try
            {
                open();
                command = new SqlCommand(query, connection);
                command.ExecuteNonQuery();
                return true;
            }
            catch (SqlException e)
            {
                //MessageBox.Show(e.ToString());
                return false;
            }
            finally
            {
                close();
            }
        }


  //using (SqlConnection con = new SqlConnection(db.connString))
  //                  {
  //                      using (SqlCommand cmd = new SqlCommand("addFile", con))
  //                      {
  //                          cmd.CommandType = CommandType.StoredProcedure;
  //                          cmd.Parameters.AddWithValue("@File", c.File);
  //                          cmd.Parameters.AddWithValue("@Cabinet", c.Cabinet);
  //                          cmd.Parameters.AddWithValue("@Drawer", c.Drawer);
  //                          cmd.Parameters.AddWithValue("@Envelope", c.Envelope);
  //                          cmd.Parameters.AddWithValue("@Page", c.Page);
  //                          cmd.Parameters.AddWithValue("@Name", "New");
  //                          cmd.Parameters.AddWithValue("@Status", "IN");
  //                          cmd.Parameters.AddWithValue("@Date", DateTime.Now);
  //                          con.Open();
  //                          cmd.ExecuteNonQuery();
  //                          MessageBox.Show("Save Successful", "Notification");
  //                          return true;

  //                      }


  //                  }


                    }
        }
    
  • 如何执行sqlconnection和sqlcommand?
  • 如何在不使用 cmd.parameters 的情况下调用 sp?
  • sqlconnection/sqlcommand 通过 2 个 DB-bound 方法
  • 如何读行?
4

2 回答 2

0
cboCabinet.Items.Insert(0, "");
            DataTable cabinetList = LI.listCabinet();
            for (int x = 0; x < cabinetList.Rows.Count; x++)
            {
                cboCabinet.Items.Add(cabinetList.Rows[x]["Cabinet"]);
            }
            List<object> list = new List<object>();
            foreach (object o in cboCabinet.Items)
            {
                if (!list.Contains(o))
                {
                    list.Add(o);
                }
            }
            cboCabinet.Items.Clear();
            cboCabinet.Items.AddRange(list.ToArray());
于 2013-05-13T22:01:25.660 回答
0
private string name = "";

public string Name
{
    get{ return name; }
    set{ name = value; } 
}
于 2013-05-13T12:00:19.070 回答