我想使用 VBA 代码从 Excel 中调用使用 WCF 开发的 Web 服务。我们应该怎么做 ?我已经尝试过该GetObject()
方法,但在使用此方法时出现语法错误。我想在 Excel 中显示从 Web 服务获取的数据。请帮忙。数据合同如下:`
[DataContract]
public class Data
{
[DataMember]
public int Id;
[DataMember]
public DateTime LockTime;
[DataMember]
public DateTime LoginTime;
[DataMember]
public DateTime LastDefenitionDate;
[DataMember]
public string NTLogin;
[DataMember]
public string SystemName;
}
服务合约如下:
`
[ServiceContract]
interface IDataService
{
[OperationContract]
List<Data> GetData();
[OperationContract]
void SubmitData(Data data);
}
`
访问数据库的DataService如下:
`
[服务行为(InstanceContextMode=InstanceContextMode.Single)]
公共类数据服务:IDataService { 公共静态 SQLConnection SQLDBConnection = new SQLConnection();
#region IDataService Members
public List<Data> GetData()
{
List<Data> datas = new List<Data>();
try
{
if (SQLDBConnection.con.State == ConnectionState.Closed) SQLDBConnection.con.Open();
datas.Clear();
SqlCommand sqlcommand = new SqlCommand();
sqlcommand.Connection = SQLDBConnection.con;
sqlcommand.CommandText = "select * from tblData";
sqlcommand.CommandType = CommandType.Text;
SqlDataAdapter sqladapter = new SqlDataAdapter(sqlcommand);
DataTable dt = new DataTable();
sqladapter.Fill(dt);
Data data = null;
for (int i = 0; i < dt.Rows.Count; i++)
{
data = new Data();
data.Id = Convert.ToInt32(dt.Rows[i]["id"]);
data.NTLogin = dt.Rows[i]["NTLogin"].ToString();
data.SystemName = dt.Rows[i]["SystemName"].ToString();
data.LockTime = Convert.ToDateTime(dt.Rows[i]["LockTime"]);
data.LoginTime = Convert.ToDateTime(dt.Rows[i]["LoginTime"]);
data.LastDefenitionDate = Convert.ToDateTime(dt.Rows[i]["LastDefenitionDate"]);
datas.Add(data);
}
}
catch (Exception ex)
{ }
return datas;
}
public void SubmitData(Data data)
{
if (SQLDBConnection.con.State == ConnectionState.Closed) SQLDBConnection.con.Open();
SqlCommand sqlcommand = new SqlCommand();
sqlcommand.Connection = SQLDBConnection.con;
sqlcommand.CommandText = "Insert into dbo.tblData(NTLogin, SystemName, LockTime, LoginTime, LastDefenitionDate) values ('" + data.NTLogin + "','" + data.SystemName + "','" + data.LockTime + "' , '" + data.LoginTime + "', '" + data.LastDefenitionDate + "')";
sqlcommand.CommandType = CommandType.Text;
int RowsAffected = sqlcommand.ExecuteNonQuery();
}
#endregion
}
`
编辑:
可能的重复建议的答案对我来说没有奏效。我的整个代码在这里给出。在发布我的问题之前,我什至检查了该帖子。请检查我的代码。