有人可以告诉我有什么问题吗?
环境 :
dbConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\data\db.mdf;Initial Catalog=db;Integrated Security=True"
类 db.cs :
public SqlConnection conn = new SqlConnection(Properties.Settings.Default.dbConnectionString);
public object insert(String[] inserts, String table)
{
string query = "INSERT INTO "+table+"";
int dim = inserts.Length;
string[] valores = new String[dim];
string[] campos = new String[dim];
string camposItems = "(";
string valoresItems = "(";
for (int i = 0; i <= dim - 1; i++)
{
string[] item = inserts[i].Split('=');
if (i > 0)
{
campos[i] = item[0];
valores[i] = item[1];
camposItems += ","+item[0];
valoresItems += ",@"+item[0];
}
else
{
campos[i] = item[0];
valores[i] = item[1];
camposItems += item[0];
valoresItems += "@"+item[0];
}
}
camposItems += ")";
valoresItems += ")";
query += camposItems+" VALUES "+valoresItems;
query += "";
try
{
using (this.conn)
{
SqlCommand cmdIns = new SqlCommand(query, this.conn);
int ix = 0;
string[] vals = new String[valores.Length];
foreach (string val in valores)
{
vals[ix] = val;
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@" + campos[ix];
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = val;
cmdIns.Parameters.Add(parameter);
ix++;
}
cmdIns.Connection.Open();
//this.conn.Open();
//object insertID = cmdIns.ExecuteNonQuery();
object insertID = cmdIns.ExecuteScalar();
cmdIns.Connection.Close();
//this.conn.Close();
if (insertID != null)
{
return insertID;
}
else
{
return false;
}
}
}
catch (Exception ex)
{
return ex;
}
}
调用函数:
db db = new db();
String[] valores = new String[2]
{
"usuario=" + this.textBox1.Text,
"clave=" + this.maskedTextBox1.Text
};
object insert = db.insert(valores, "usuarios_tb");
请帮忙谢谢...