1提取单条记录
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
SqlCommand cmd = new SqlCommand("Select Count() From jobs",cn);
cnOpen();
%%2=cmdExecuteScalar(); //MessageInnerHtml
}
2单值比较
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
SqlCommand cmd = new SqlCommand("Select Count() From jobs",cn);
cnOpen();
%%2=cmdExecuteScalar(); //MessageInnerHtml
if(%%2==%%3)
{
%%4
}
}
3显示表格
//using SystemData;
//using SystemDataSqlClient;
DataSet ds=null;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
cnOpen();
cmd=new SqlDataAdapter("Select From Author",cn);
ds=new DataSet();
cmdFill(ds,%%2); //"作者"
MyDataGridDataSource=dsTables(%%2)DefaultView; //"作者"
MyDataGridDataBind();
}
4 *** 作表格
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
cnOpen();
cmd=new SqlDataAdapter("Select From Author",cn);
ds=new DataSet();
cmdFill(ds,%%2); //"作者"
MyDataGridDataSource=dsTables(%%2)DefaultView; //"作者"
MyDataGridDataBind();
}
5数值范围查询
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
}
6关闭时断开连接
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
}
7执行命令
//using SystemData;
//using SystemDataSqlClient;
using (SqlConnection cn = new SqlConnection(%%1))
//ConfigurationManagerConnectionStrings["db2ConnectionString"]ConnectionString
//Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
{
SqlCommand cmd = new SqlCommand("insert userRegister_t values('"
+ TextBox1Text + "','" + TextBox2Text + "')",cn);
cnOpen();
cmdExecuteNonQuery();
}
7Oracle8/8i/9i数据库(thin模式)
//using SystemData;
Oracle Set Your custom connection strings values
ODBC
New version
Driver={Microsoft ODBC for Oracle};Server=myServerAddress;Uid=myUsername;Pwd=myPassword;
Old version
Driver={Microsoft ODBC Driver for Oracle};ConnectString=OracleServerworld;Uid=myUsername;Pwd=myPassword;
OLE DB, OleDbConnection (NET)
Standard security
This connection string uses a provider from Microsoft
Provider=msdaora;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;
Standard Security
This connection string uses a provider from Oracle
Provider=OraOLEDBOracle;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;
Trusted Connection
Provider=OraOLEDBOracle;Data Source=MyOracleDB;OSAuthent=1;
OracleConnection (NET)
Standard
Data Source=MyOracleDB;Integrated Security=yes;
This one works only with Oracle 8i release 3 or later
Specifying username and password
Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;Integrated Security=no;
This one works only with Oracle 8i release 3 or later
Missing the SystemDataOracleClient namespace Download NET Managed Provider for Oracle >>
Great article! "Features of Oracle Data Provider for NET" by Rama Mohan G at C# Corner >>
Omiting tnsnamesora
This is another type of Oracle connection string that doesn't rely on you to have a DSN for the connection You create a connection string based on the format used in the tnsnamesora file without the need to actually have one of these files on the client pc
SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort))(CONNECT_DATA=(SERVICE_NAME=MyOracleSID)));uid=myUsername;pwd=myPassword;
Core Labs OraDirect (NET)
Standard
User ID=myUsername;Password=myPassword;Host=ora;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;
Read more at Core Lab >>
And at the product page >>
Data Shape
MS Data Shape
Provider=MSDataShape1;Persist Security Info=False;Data Provider=MSDAORA;Data Source=orac;User Id=myUsername;Password=myPassword;
16程序计时
DateTime runTime = ConvertToDateTime(SystemConfigurationConfigurationSettingsAppSettings["TimerRunAt"]);
//运行时间
TimeSpan runTime = new TimeSpan(runTimeTicks);
//现在时间
TimeSpan timeNow = new TimeSpan(DateTimeNowTicks);
//时间间隔
TimeSpan ts = runTimeSubtract(timeNow);
//如果运行时间与当前时间的差大于0,则任务执行时间为当日
if(tsTotalMilliseconds > 0)
{
return tsTotalMilliseconds;
}
//如果运行时间与当前时间的差小于0,则任务执行时间为次日
Else
{
//差值的绝对值应加1天
return tsDuration()Add(new TimeSpan(1,0,0,0,0))TotalMilliseconds;
}
17延时
//using SystemThreading;
ThreadSleep(Int32Parse(%%1));
18连接Excel文件
string connString = "Provider=MicrosoftJetOLEDB40;Data Source=" + excel文件名(绝对路径) +";Extended Properties=Excel 80;";
连接MYSQL数据库的方法及示例
方法一:
使用MYSQL推出的MySQL
Connector/Net
is
an
ADONET
driver
for
MySQL
该组件为MYSQL为ADONET访问MYSQL数据库设计的NET访问组件。
安装完成该组件后,引用命名空间MySqlDataMySqlClient;
使用命令行编译时:csc
/r:MySqlDatadll
testcs
方法二:
通过ODBC访问MYSQL数据库
访问前要先下载两个组件:odbcnet和MYSQL的ODBC驱动(MySQL
Connector/ODBC
(MyODBC)
driver)目前为351版
安装完成后,即可通过ODBC访问MYSQL数据库
方法三:
使用CoreLab推出的MYSQL访问组件,面向NET
安装完成后,引用命名空间:CoreLabMySql;
使用命令编译时:csc
/r:CoreLabMySqldll
testcs
以下为访问MYSQL数据库实例
编译指令:csc
/r:CoreLabMySqldll
/r:MySqlDatadll
testcs
using
System;
using
SystemNet;
using
SystemText;
using
CoreLabMySql;
using
SystemDataOdbc;
using
MySqlDataMySqlClient;
class
ConnectMySql
{
public
void
Connect_CoreLab()
{
string
c;
MySqlConnection
mycn
=
new
MySqlConnection(constr);
mycnOpen();
MySqlCommand
mycm
=
new
MySqlCommand("select
from
shop",mycn);
MySqlDataReader
msdr
=
mycmExecuteReader();
while(msdrRead())
{
if
(msdrHasRows)
{
ConsoleWriteLine(msdrGetString(0));
}
}
msdrClose();
mycnClose();
}
public
void
Connect_Odbc()
{
//string
MyC;
string
MyC
+
"SERVER=localhost;"
+
"DATABASE=test;"
+
"UID=root;"
+
"PASSWORD=qing;"
+
"OPTION=3";
OdbcConnection
MyConn
=
new
OdbcConnection(MyConString);
MyConnOpen();
OdbcCommand
mycm
=
new
OdbcCommand("select
from
hello",MyConn);
OdbcDataReader
msdr
=
mycmExecuteReader();
while(msdrRead())
{
if
(msdrHasRows)
{
ConsoleWriteLine(msdrGetString(0));
}
}
msdrClose();
MyConnClose();
}
public
void
Connect_Net()
{
string
myC;
MySqlConnection
mycn
=
new
MySqlConnection(myConnectionString);
mycnOpen();
MySqlCommand
mycm
=
new
MySqlCommand("select
from
hello",mycn);
MySqlDataReader
msdr
=
mycmExecuteReader();
while(msdrRead())
{
if
(msdrHasRows)
{
ConsoleWriteLine(msdrGetString(0));
}
}
msdrClose();
mycnClose();
}
public
static
void
Main()
{
ConnectMySql
ms
=
new
ConnectMySql();
msConnect_CoreLab();
msConnect_Odbc();
Connect_Net();
}
}
缺少dll net core 版本 有点问题 引用的dll 不会自动输出到项目目录下去,需要手动拷过去,或者在项目里指定复制到项目目录 ,你看看你的mysql是不是引用了第三方的库 并且在输出目录下没有生成这个dll ,好像在vs2017 153以后就没有这个问题了
缺少dll net core 版本 有点问题 引用的dll 不会自动输出到项目目录下去,需要手动拷过去,或者在项目里指定复制到项目目录 ,你看看你的mysql是不是引用了第三方的库 并且在输出目录下没有生成这个dll ,好像在vs2017 153以后就没有这个问题了
以上就是关于c#连接SQL数据库的使用方法全部的内容,包括:c#连接SQL数据库的使用方法、用C#.net连接MYSQL,怎么连接还要安装什么吗急!!!、C# 连接数据库时 系统找不到指定的文件。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)