C#VS连接数据库DBhelp
MySql数据库连接
public static string connStr = "Database=AliceDB;Data Source=127.0.0.1;Port=3306;UserId=root;Password=123456;Charset=utf8;TreatTinyAsBoolean=false;Allow User Variables=True";
MySqlHelper的⽅法:
insert & update
public static bool UpdateOpera(string sql)
{
MySqlConnection connection = new MySqlConnection(connStr);//实例化链接
connection.Open();//开启连接
MySqlCommand cmd = new MySqlCommand(sql, connection);
try
{
cmd.CommandText = sql;
return cmd.ExecuteNonQuery() > 0;
}
catch (System.Exception ex)
{
connection.Close();
Console.WriteLine(ex.ToString());
return false;
}
finally
{
connection.Close();
}
}
Orcl数据库连接
public static string connStr = "User Id=Alice;Password=123456;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=root)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME = orcl))) OrclHelper的⽅法:
select & insert & update
#region执⾏查询全部SQL语句,返回DataTable;
查询全部
DataTable dt = OracleHelper.ExecuteDataTable(sqlString);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow mydr = dt.Rows[i];
string email = mydr["email"].ToString();
}
public static DataTable ExecuteDataTable(string sql, params OracleParameter[] parameters)
{
OracleConnection connection = new OracleConnection(connStr);
OracleCommand cmd = new OracleCommand(sql, connection);
try
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
DataTable datatable = new DataTable();
adapter.Fill(datatable);
return datatable;
}
catch (System.Exception ex)
{
connection.Close();
//Console.WriteLine(ex.ToString());
return null;
}
finally
{
connection.Close();
}
}
#endregion
#region执⾏插⼊或修改SQL语句,返回True & false;
if (OracleHelper.UpdateOpera(updateStr))
{
MessageBox.Show("成功")
}
public static bool UpdateOpera(string sql, params OracleParameter[] parameters)
{
OracleConnection connection = new OracleConnection(connStr);
OracleCommand cmd = new OracleCommand(sql, connection);
try
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
cmd.CommandText = sql;
return cmd.ExecuteNonQuery() > 0;
}
catch (System.Exception ex)
{
connection.Close();
//Console.WriteLine(ex.ToString());
return false;
}
finally
{
connection.Close();
}
}
#endregion
#region执⾏查询SQL语句,返回是否存在;
OracleDataReader reader = OracleHelper.ExecuteReader(sqlCount);
while (reader.Read())
{
//存在去修改
}
public static OracleDataReader ExecuteReader(string strSQL, params OracleParameter[] parameters) {
connection = new OracleConnection(connStr);
OracleCommand cmd = new OracleCommand(strSQL, connection);
try
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
OracleDataReader myReader = cmd.ExecuteReader();
mysql下载的vs库放在那个文件里return myReader;
}
catch (OracleException e)
{
connection.Close();
throw new Exception(e.Message);
}
/
/finally
//{
// connection.Close();
//}
}
#endregion
Sqlserver数据库连接
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论