我有一个从csv文件动态创建的数据集.我想要做的是将行插入我的MS Access表,但我无法弄清楚从哪里开始.
数据集中数据的标题可以根据顺序而变化,但标题的名称将始终与访问数据库匹配.我是否必须在insert命令中静态调用标题名称,还是可以从数据集中构建标题?
我知道如何创建连接并将其打开到数据库但不知道如何在insert命令中创建动态拉动表头.
在C#编程方面我很绿,所以如果你能为我拼出来,我真的很感激!
以下是访问表标头的示例:
ID,项目,成本,零售
然后是CSV,它将填充数据集表.它可能有零售或可能没有:
物品,成本
这是我到目前为止的代码,但它没有写入访问表.如果我发现dtAccess它正确显示.
OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"C:\\Database.accdb\";Persist Security Info=False;");
myConnection.Open();
string queryString = "SELECT * from " + lblTable.Text;
OleDbDataAdapter adapter = new OleDbDataAdapter(queryString, myConnection);
DataTable dtAccess = new DataTable();
DataTable dtCSV = new DataTable();
dtCSV = ds.Tables[0];
using (new OleDbCommandBuilder(adapter))
{
adapter.Fill(dtAccess);
dtAccess.Merge(dtCSV);
adapter.Update(dtAccess);
}
myConnection.Close();
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用HTML Agility包从站点中抓取一些数据.我真的在努力弄清楚如何在foreach中使用selectnodes,然后将数据导出到列表或数组.
这是我到目前为止使用的代码.
string result = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(http://www.amazon.com/gp/offer-listing/B002UYSHMM/);
request.Method = "GET";
using (var stream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(new StringReader(result));
HtmlNode root = doc.DocumentNode;
string itemdesc = doc.DocumentNode.SelectSingleNode("//h1[@class='producttitle']").InnerText; //this works perfectly to get the title of the item
//HtmlNodeCollection sellers = doc.DocumentNode.SelectNodes("//id['bucketnew']/div/table/tbody/tr/td/ul/a/img/@alt");//this does not work at all in getting the alt attribute from the seller images
HtmlNodeCollection prices = doc.DocumentNode.SelectNodes("//span[@class='price']"); //this works …Run Code Online (Sandbox Code Playgroud) 这可能是一个简单而愚蠢的问题,但我似乎无法找到有关选择具有多个属性的节点的任何内容.在我的例子中,它是一个特定的类和一个特定的风格.
这是我正在使用的HTML的片段.
<div class="buying" style="padding-bottom: 0.75em;">
<span class="availGreen">Blah Blah</span><br /> Blah Blah Blah<b>Sold By</b>.
</div>
Run Code Online (Sandbox Code Playgroud)
类"购买"有许多不同的实例,但只有一个div的实例包括购买类和style ="padding-bottom:0.75em属性.我试图抓取标签内的文本.
这是我尝试过但我无处可寻:
SelectSingleNode("//div[@class='buying'][@style='padding-bottom: 0.75em;']/b").InnerText;
Run Code Online (Sandbox Code Playgroud)
并且:
SelectSingleNode("//div[@class='buying' @style='padding-bottom: 0.75em;']/b").InnerText;
Run Code Online (Sandbox Code Playgroud)
这些都没有产生任何结果,但我不确定还有什么是正确的.
任何帮助深表感谢!
我正在尝试使用ILMerge将我的C#程序与3个引用的DLL结合起来.如果我运行程序而不合并它们,一切运行良好,但当我合并它们时,我得到"Void System.Threading.Monitor.Enter"错误.
这是我正在组合的DLL:
HTMLAgilityPack.dll
MySql.Data.dll
RKLib.ExportData.dll
Run Code Online (Sandbox Code Playgroud)
错误似乎来自MySql.Data.dll但我不确定为什么会抛出此异常.
任何想法都非常感激.
编辑:我收到的完整错误是:
************** Exception Text **************
System.MissingMethodException: Method not found: 'Void System.Threading.Monitor.Enter(System.Object, Boolean ByRef)'.
at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value)
at MySql.Data.MySqlClient.MySqlConnection..ctor(String connectionString) in :line 0
Run Code Online (Sandbox Code Playgroud) 我正在学习C#,并且遇到了加载系统事件的问题.它工作正常,直到我将form1和form2链接在一起以在它们之间传递变量.IE在form2上的选定项目上设置Form 1上的标签.谢谢你的帮助!
这是Form1的代码:
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
selectdb selectdb = new selectdb(this); // this is the button that shows the form in question. It worked fine until I added the (this).
selectdb.Show();
}
public string LabelText
{
get { return label1.Text; }
set { label1.Text = value; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Form2代码:
namespace WindowsFormsApplication5
{
public partial class selectdb : Form
{ …Run Code Online (Sandbox Code Playgroud) 我试图使用简单的php html dom解析器从img标签中仅拉出alt值.我似乎无法将其拉到alt标签.这是我正在使用的:
foreach($html->find('ul.sellerInformation img', 0) as $element) {
$ret['SoldBy'] = $element->alt;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用它,它可以工作并拉出整个图像标记:
$ret['SoldBy'] = $html->find('ul.sellerInformation img', 0)->outertext;
Run Code Online (Sandbox Code Playgroud)
但我真正想要的只是alt标签中的信息.
我正试图从亚马逊优惠列表页面的html中获取:http://www.amazon.com/gp/offer-listing/B001H8QHG0
有什么建议?
谢谢您的帮助!
我试图从foreach每个迭代循环调用一个方法,并将每次迭代更新的字符串传递给方法.如何获取我调用的方法来获取字符串?
这是我正在测试的内容:
foreach (DataRow dr in dt.Rows)
{
string url = "http://www.amazon.com/gp/offer-listing/"+dr["ASIN"].ToString();
updatedatabase(url);
}
Run Code Online (Sandbox Code Playgroud)
使用此方法:
private void updatedatabase(url)
{
MessageBox.Show(url);
}
Run Code Online (Sandbox Code Playgroud)