您的位置:首页 > 数据库

数据库连接,查询和插入数据的方法

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: