【发布时间】:2019-11-02 04:48:57
【问题描述】:
我有一个保存个人信息的 MySQL 数据库。每当新员工被录用时,他/她都会填写一些个人信息,然后这些数据就会存储在表格中。
经过一些研究(并且由于我无法访问其他系统 - 只有数据库),计划是构建一个 C# 控制台应用程序来检索数据并根据 SharePoint 列表进行检查。如果数据库中有一条新记录在以前的 SharePoint 列表中不存在,我想更新列表(创建一个新项目)。
请注意,如果 SharePoint 列表包含更多列,则该表包含额外的手动信息。
我已经发布了针对数据库的连接代码以及我如何检索数据。
如何检查项目是否存在于 SharePoint 列表中?谁能提供一个包含创建和插入新项目的代码的答案?我有两列(在数据库和 SP 列表中)可以用作主键。
有一个支持 CRUD 的 REST API,所以我想这应该是显而易见的。
using System;
using System.Windows;
public class DbConnection
{
private String databaseName;
private String serverAddress;
private String pwd;
private String userName;
private Boolean connected;
private MySql.Data.MySqlClient.MySqlConnection conn;
public DbConnection(String databaseName, String serverAddress, String pwd, String userName)
{
this.databaseName = databaseName;
this.serverAddress = serverAddress;
this.pwd = pwd;
this.userName = userName;
connected = false;
}
public void Connect()
{
if (connected == true)
{
Console.Write("There is already a connection");
}
else
{
connected = false;
String connectionString = "server=" + serverAddress + ";" + "database=" + databaseName + ";" + "uid=" + userName + ";" + "pwd=" + pwd + ";";
Console.WriteLine(connectionString);
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
conn.Open();
Console.Write("Connection was succesfull");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
public Boolean IsConnected()
{
return connected;
}
public MySql.Data.MySqlClient.MySqlConnection getConnection()
{
return conn;
}
public void Close()
{
conn.Close();
connected = false;
}
}
然后我像这样检索数据:
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace daily_CC_SP_update
{
class Program
{
static void Main()
{
DbConnection mySQLConn = new DbConnection(dbName, serverAddress, pwd, userName);
mySQLConn.Connect();
string sqlQuery = "SELECT * FROM tbl_CC_SP";
MySqlCommand sqlCom = new MySqlCommand(sqlQuery, mySQLConn.getConnection());
MySqlDataReader reader = sqlCom.ExecuteReader();
Console.WriteLine("Following output from DB");
if(reader.Read())
{
Console.WriteLine(reader.GetString(0));
}
//Keep the console alive until enter is pressed, for debugging
Console.Read();
mySQLConn.Close();
}
}
}
我将在数据库中创建一个视图来检索正确的数据。
【问题讨论】:
标签: c# mysql rest sharepoint-online sharepoint-designer