数据库连接,查询和插入数据的方法
2012-08-22 14:58
591 查看
关于数据库的三个方法
第一个是连接数据库
第二个是查询数据库并以table形式返回
第三个是插入数据库
public SqlConnection Connection(string str_sql)
{
string str = "server=.;user id=sa;password='000000';database=CRCData";
SqlConnection sql_conn = new SqlConnection(str);
return sql_conn;
}
public DataTable SelectDataBySql(string str_sql)
{
SqlConnection sql_conn = Connection(str_sql);
SqlDataAdapter Adapter = new SqlDataAdapter(str_sql, sql_conn);
DataSet ds = new DataSet();
Adapter.Fill(ds);
return ds.Tables[0];
}
public int InsertDataBySql(string str_sql)
{
SqlConnection conn = Connection(str_sql);
SqlCommand Command = new SqlCommand();
Command.Connection = conn;
Command.CommandType = CommandType.Text;
Command.CommandText = str_sql;
conn.Open();
int result = Command.ExecuteNonQuery();
conn.Close();
return result;
}
第一个是连接数据库
第二个是查询数据库并以table形式返回
第三个是插入数据库
public SqlConnection Connection(string str_sql)
{
string str = "server=.;user id=sa;password='000000';database=CRCData";
SqlConnection sql_conn = new SqlConnection(str);
return sql_conn;
}
public DataTable SelectDataBySql(string str_sql)
{
SqlConnection sql_conn = Connection(str_sql);
SqlDataAdapter Adapter = new SqlDataAdapter(str_sql, sql_conn);
DataSet ds = new DataSet();
Adapter.Fill(ds);
return ds.Tables[0];
}
public int InsertDataBySql(string str_sql)
{
SqlConnection conn = Connection(str_sql);
SqlCommand Command = new SqlCommand();
Command.Connection = conn;
Command.CommandType = CommandType.Text;
Command.CommandText = str_sql;
conn.Open();
int result = Command.ExecuteNonQuery();
conn.Close();
return result;
}
相关文章推荐
- Java连接HBASE数据库,创建一个表,删除一张表,修改表,输出插入,修改,数据删除,数据获取,显示表信息,过滤查询,分页查询,地理hash
- 使用PDO连接数据库 查询和插入乱码的解决方法
- 使用PDO连接数据库 查询和插入乱码的解决方法
- 使用PDO连接数据库 查询和插入乱码的解决方法
- JDBC连接数据库方法的封装,以及查询数据方法的封装(精华)
- JDBC连接数据库方法的封装,以及查询数据方法的封装
- 连接数据库及数据查询的简单实现方法
- 使用PDO连接数据库 查询和插入乱码的解决方法
- 面面观 | 使用python 连接数据库,插入并查询数据--link
- JDBC 数据库连接 创建表格、插入、查询、删除、修改数据 基本操作
- docker(7):使用python 连接数据库,插入并查询数据--link
- 数据库左连接查询(查询多张表中的数据)
- 数据库插入数据返回当前主键ID值方法
- 各种数据库查询前几条数据的方法
- Java:jdbc连接数据库插入中文数据乱码问题
- ThinkPHP 中 M 方法查询数据库最后使用 find() 导致返回结果集 foreach 数据不对的问题
- 把数据插入到数据库的两种方法
- java连接memcached,并对比从数据库中查询数据与从memcached查询的性能
- linux shell脚本连接oracle查询数据插入文件和日志文件中
- java JDBC 连接数据库查询数据与直接使用sql的疑问