总是写Parameter我觉得麻烦,于是我想出了吧他用字符窜的方式表示
然后分解,功能实现,帮我看看@"select * from table where 1=1 and con=@Con and con=@Con2 and con=@adsfasdf#int,varchar,int#4,10,4#"
public SqlParameter[] GetParameter(string sql, params object[] value)
{
var sp = new SqlParameter[value.Length];
string[] name = new string[value.Length];
string[] y = sql.Split('#');
string[] type = y[1].Split(',');
string[] number = y[2].Split(',');
char[] cht = y[0].ToCharArray();
bool one = true;
bool two = true;
int z = 0;
int s = 0;
while (one)
{
if (z < y[0].Length)
{
two = true;
z = y[0].IndexOf('@', z);
while (two)
{
if (cht[z] != ' ')
{
name[s] += cht[z];
z++;
if (!(z >= y[0].Length))
{
if (cht[z] == ' ')
{
two = false;
s++;
}
}
else
{
two = false;
one = false;
}
}
}
}
else
{
one = false;
}
} for (int i = 0; i < value.Length; i++)
{
sp[i] = new SqlParameter(name[i], SqlType(type[i]), Convert.ToInt32(number[i]));
sp[i].Value = value[i];
} return sp;
}
然后分解,功能实现,帮我看看@"select * from table where 1=1 and con=@Con and con=@Con2 and con=@adsfasdf#int,varchar,int#4,10,4#"
public SqlParameter[] GetParameter(string sql, params object[] value)
{
var sp = new SqlParameter[value.Length];
string[] name = new string[value.Length];
string[] y = sql.Split('#');
string[] type = y[1].Split(',');
string[] number = y[2].Split(',');
char[] cht = y[0].ToCharArray();
bool one = true;
bool two = true;
int z = 0;
int s = 0;
while (one)
{
if (z < y[0].Length)
{
two = true;
z = y[0].IndexOf('@', z);
while (two)
{
if (cht[z] != ' ')
{
name[s] += cht[z];
z++;
if (!(z >= y[0].Length))
{
if (cht[z] == ' ')
{
two = false;
s++;
}
}
else
{
two = false;
one = false;
}
}
}
}
else
{
one = false;
}
} for (int i = 0; i < value.Length; i++)
{
sp[i] = new SqlParameter(name[i], SqlType(type[i]), Convert.ToInt32(number[i]));
sp[i].Value = value[i];
} return sp;
}
Split(空)然后在IndexOf也呢省电事情吧
Split(new char{‘#’,‘,’})
楼下留给Lanboa
public SqlParameter[] GetParameter(string sql, params object[] value)
{
string[] y = sql.Split('#');
string[] type = y[1].Split(',');
string[] number = y[2].Split(',');
var sp = new SqlParameter[value.Length];
MatchCollection matches = new Regex(@"[^@@](?<X>@\w+)").Matches(String.Concat(y[0], " "));
for (int i = 0; i < value.Length; i++)
{
sp[i] = new SqlParameter(matches[i].Groups["X"].Value, SqlType(type[i]), Convert.ToInt32(number[i]));
sp[i].Value = value[i];
}
return sp;
}吧类型去了也没什么关系,我个人感觉,迎接板砖:}
/// 更新数据库记录(SQL句柄)
/// </summary>
/// <param name="strSql">要执行的SQL语句</param>
/// <returns>执行成功返回True,否则返回False</returns>
public override bool ExecuteUpdate(string strSql)
{
try
{
Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = this.conn;
if (inTransaction)
cmd.Transaction = trans;
cmd.CommandText = strSql;
cmd.CommandType = CommandType.Text;
return cmd.ExecuteNonQuery() > 0;
}
catch
{
return false;
}
finally
{
this.conn.Close();
}
} /// <summary>
/// 更新数据库记录(存储过程)
/// </summary>
/// <param name="strSql">SQL句柄</param>
/// <param name="strParams">存储过程变量集</param>
/// <param name="strValues">存储过程内容集</param>
/// <returns>执行成功返回True,否则返回False</returns>
public override bool ExecuteUpdate(string strSql,string[] strParams,object[] strValues)
{
try
{
Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = this.conn;
if (inTransaction)
cmd.Transaction = trans;
if ((strParams != null) && (strParams.Length != strValues.Length))
throw new Exception("查询参数和值不对应!");
cmd.CommandText = strSql;
cmd.CommandType = CommandType.Text;
if (strParams != null)
{
for (int i = 0; i < strParams.Length; i++)
{
SqlParameter param = new SqlParameter(strParams[i], strValues[i]);
cmd.Parameters.Add(param); }
}
return cmd.ExecuteNonQuery() > 0;
}
catch
{
return false;
}
finally
{
this.conn.Close();
}
}string[] strpar={"@id"}
object[] strval={id}
ExecuteUpdate(query,strpar,strval);