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 方法
- 如何读行?