vs2008中c++ ado连接sql server数据库后如何使用带参数的sql语句

2024-12-02 10:49:32
推荐回答(2个)
回答(1):

用vs2008 只会写C#的。。
C++不会啊
这是我C#的更新操作
public void EditAccount(Account bankAccount)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update BankAccount set ");
strSql.Append("AccountName=@AccountName,");
strSql.Append("CurrentAmount=@CurrentAmount,");
strSql.Append("IsCanceled=@IsCanceled");
strSql.Append(" where CardNo=@CardNo ");
SqlParameter[] parameters = {
new SqlParameter("@AccountName", SqlDbType.NVarChar,1024),
new SqlParameter("@CurrentAmount", SqlDbType.Decimal,9),
new SqlParameter("@IsCanceled", SqlDbType.Bit,1),
new SqlParameter("@CardNo", SqlDbType.NVarChar,100)};

parameters[0].Value = bankAccount.Cname;
parameters[1].Value = bankAccount.Csum;
parameters[2].Value = bankAccount.Cfreeze;
parameters[3].Value = bankAccount.Cid;

using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
cmd.CommandType = CommandType.Text;
connection.Open();
cmd.Connection = connection;
cmd.CommandText = strSql.ToString();
foreach (SqlParameter parameter in parameters)
{
cmd.Parameters.Add(parameter);
}
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
catch (System.Data.SqlClient.SqlException e)
{
throw e;
}
}
}
}

回答(2):

select * from 你要查询的表 where id='+@id+' 是想要这样的么?