【c#】字符串的拼接
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了【c#】字符串的拼接相关的知识,希望对你有一定的参考价值。
根据两个参数来拼接SQL字符串,首先判断变量type,如果有值字符串为where type='type量' 其次判断key是否有值,若有值字符串为where key='key量',这里有一个问题,假如前面的type有值的话,后面的key就要变成and key='key值',反之使用字符串where key='key值'
以下是我的拼接字符串:
string sql=string.Format("select count(*) from tb_product 0",(type!="")?(" where type='" + type + "'"):(key!=""&&type!="")?(" and key="+key):( " where key="+key));
但我总感觉这样写不太正规,不知道哪位大哥指点一下,这种拼接如何写能规范些!先谢谢了!
我是这在拼接sql语句
string cname="";
string csex="";
string cdate="";
string sql=string.Format("select count(*) from tb_tablename where name=0 and sex=1 order by [3]",cname,csex,cdate) 参考技术B 字符串的拼接用此符号就行:&
c#连接到mysql 的连接字符串怎么写
参考技术A C#版mysql连接字符串ODBC DSN
ODBC -- MyODBC Driver -- local database
ODBC -- MyODBC Driver -- remote database
ODBC -- MySQL ODBC 3.51 Driver
OleDb
.NET DataProvider from CoreLab
// ODBC DSN
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
"Dsn=DsnName;" +
"Uid=UserName;" +
"Pwd=Secret;";
conn.Open();
// ODBC -- MyODBC Driver -- local database
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
"Driver=MySql;" +
"Server=localhost;" +
"Option=16834;" +
"DataBase=DataBaseName;"
conn.Open();
// ODBC -- MyODBC Driver -- remote database
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
"Driver=MySql;" +
"Server=db.domain.com;" +
"Option=131072;" +
"Port=3306;" +
"Stmt=;" +
"DataBase=DataBaseName;" +
"Uid=UserName;" +
"Pwd=Secret;"
conn.Open();
// ODBC -- MySQL ODBC 3.51 Driver
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
"Driver=MySql ODBC 3.51 Driver;" +
"Server=ServerName;" +
"Option=16834;" +
"Port=3306;" +
"Stmt=;" +
"DataBase=DataBaseName;" +
"Uid=UserName;" +
"Pwd=Secret;"
conn.Open();
// or
using System.Data.Odbc;
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
"DRIVER=MySql ODBC 3.51 Driver;" +
"SERVER=ServerName;" +
"DATABASE=DataBaseName;" +
"USER=UrerName;" +
"PASSWORD=Secret;"
conn.Open();
// OleDb
using System.Data.OleDb;
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString =
"Provider=MySqlProv;" +
"Data Source=ServerName;" +
"User id=UserName;" +
"Password=Secret;"
conn.Open();
// .NET DataProvider from CoreLab
using CoreLab.MySql;
MySqlConnection conn = new MySqlConnection();
conn.ConnectionString =
"Host=ServerName;" +
"DataBase=DataBaseName;" +
"Protocol=TCP;" +
"Port=3306;" +
"Direct=true;" +
"Compress=false;" +
"Pooling=true;" +
"Min Pool Size=0;" +
"Max Pool Size=100;" +
"Connection Lifetime=0;" +
"User id=UserName;" +
"Password=Secret;" +
conn.Open();
以上是关于【c#】字符串的拼接的主要内容,如果未能解决你的问题,请参考以下文章