sharepoint 2010:沙盒Web部件中的数据库连接

时间:2011-02-09 11:02:44

标签: visual-studio-2010 sharepoint-2010 database-connection

我想在沙盒Web部件中建立数据库连接。以下代码在我使用查询时工作正常。谁能告诉我如何使用存储过程&传递参数给它?还有如何使用Web服务?

    public static DataSet ChkConn(string strval)
    {
        public static DataSet ds;        
        public static string assemblyName = "FullTrustProxy, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=75e25e9dc5ff21aa";
        public static string typeName = "DBFullTrust.FullTrustProxy.SQLFullTrustProxy";

        try
        {
            SQLProxyArgs proxyArgs = new SQLProxyArgs();
            proxyArgs.ConnectionString = "Persist Security Info=False;User ID=sa;Password=;Initial Catalog=Cat;Data Source=ABC";
            if (strval == "B")
                proxyArgs.Command = "select * from table1";
            else
                proxyArgs.Command = "select * from table2";


            proxyArgs.returnType = typeof(DataSet);
            return ds = (DataSet)SPUtility.ExecuteRegisteredProxyOperation(assemblyName, typeName, proxyArgs);                 
        }
        catch
        {

        }
        return ds;
    }

1 个答案:

答案 0 :(得分:0)

您需要更改完整信任代理操作。

像这样采取参数

http://spc3.codeplex.com/SourceControl/changeset/view/57419#985226

using (SqlCommand cmd = new SqlCommand(args.Command, conn)) {
    command.CommandType = CommandType.StoredProcedure;
    foreach (KeyValuePair<string, object> para in args.Parameters) {
        cmd.Parameters.AddWithValue(para.Key, para.Value);
    }
    return cmd.ExecuteNonQuery();
}

并使用它来调用代理:

http://spc3.codeplex.com/wikipage?title=DatabaseProxy

SqlDataReaderProxyArgs args = new SqlDataReaderProxyArgs();
args.ConnectionString = "Data Source=;Initial Catalog=;User ID=;Password=";
args.Command = "select * from sys.indexes";
args.CommandType = CommandType.Text;
args.Parameters.Add(new KeyValuePair<string, object>() { Key = "Id", Value = "1"});
string assemblyName = typeof(SqlDataReaderProxyOperation).Assembly.FullName;
string typeName = typeof(SqlDataReaderProxyOperation).FullName;
object result = SPUtility.ExecuteRegisteredProxyOperation(assemblyName, typeName, args);