|
查看: 1754|回复: 20
|
[载图+教学]C# 用 OLEDB COMPONENT 来 载入 DATABASE
[复制链接]
|
|
|

楼主 |
发表于 27-7-2006 03:13 PM
|
显示全部楼层
偶的CLASS的例子:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
namespace WindowsApplication3
{
class UserLogin
{
private string constring;
private string sqlcom;
//string a是拿来收INSTANTIATE的CONNECTION STRING
//string b是拿来收SQL COMMAND 的
public UserLogin(string a,string b)
{
constring = a;
sqlcom = b;
//将a和b都ASSIGN到constring 和 sqlcom里面
(为了符合OBJECT ORIENTED PROGRAMMING,没法啦。。。)
}
//这个METHOD是拿来CONNECT DATABASE, RUN那个SQL,然后RETURN
//INTEGER VALUE,-1就是ERROR,1就是找到啦,0就是找没有。
public int loginexe()
{
OleDbDataReader dtrcust;
String connectionString = constring;
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(sqlcom);
command.Connection = connection;
try
{
connection.Open();
dtrcust = command.ExecuteReader();
//这里是将收到的VALUEASSIGN进去一个DATAREADER(专门那来SAVE DATA LOAD FROM DATABASE的)
//如果DATAREADER里面有RECORD,就是说读取成功啦。。。
//如果没有就是说没有酱的RECORD
bool meet = true;
for (int i = 0; i < dtrcust.FieldCount; i++)
{
if (dtrcust.Read())
{
meet = false;
}
}
if (meet==false)
return 1;
else
return 0;
}
catch (Exception ex)
{
return -1;
}
}
}
}
} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-7-2006 03:14 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-7-2006 03:14 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-7-2006 03:23 PM
|
显示全部楼层
噢,忘记跟你们讲ASP。NET如果用C#来写也可以用这个CLASS |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-7-2006 03:23 PM
|
显示全部楼层
噢,忘记跟你们讲ASP。NET如果用C#来写也可以用这个CLASS |
|
|
|
|
|
|
|
|
|
|
发表于 30-7-2006 05:11 PM
|
显示全部楼层
我试了,但有error
Type or namespace definition, or end-of-file expected
要怎样呢?
(小弟乃初学者) |
|
|
|
|
|
|
|
|
|
|
发表于 30-7-2006 05:15 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 30-7-2006 09:51 PM
|
显示全部楼层
原帖由 lieweffect 于 30-7-2006 05:15 PM 发表
还有之前的table是customer,到了initialize database 那边又变了member
不知道对不对?
噢。。。
那个素偶的ERROR!!
抱歉。。换去Customer试下看看。。 |
|
|
|
|
|
|
|
|
|
|
发表于 1-8-2006 05:02 PM
|
显示全部楼层
尝试了还table名字
但error还出现
Type or namespace definition, or end-of-file expected
要怎样呢? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-8-2006 05:18 PM
|
显示全部楼层
原帖由 lieweffect 于 1-8-2006 05:02 PM 发表
尝试了还table名字
但error还出现
Type or namespace definition, or end-of-file expected
要怎样呢?
今晚我弄弄看。。。。
顺便贴一些SCREEN SHOT 上来。。。
抱歉噢。。。现在在公司不能弄。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 1-8-2006 05:32 PM
|
显示全部楼层
原帖由 弥勒 于 1-8-2006 05:18 PM 发表
今晚我弄弄看。。。。
顺便贴一些SCREEN SHOT 上来。。。
抱歉噢。。。现在在公司不能弄。。。
3q............. |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-8-2006 07:41 PM
|
显示全部楼层
原帖由 lieweffect 于 1-8-2006 05:32 PM 发表
3q.............
偶加了SCREENSHOT了。。。
希望更多人了解咯。。。
还有。。。我RUN了。。。米有问题。。。
看你的ERROR是。。。
这个"}"好象太多或太少。。。
或者你看上面的namespace有米有
using System.Data.OleDb;
using System.Data; |
|
|
|
|
|
|
|
|
|
|
发表于 1-8-2006 10:25 PM
|
显示全部楼层
check了还是酱
我的code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace WindowsApplication1
{
class UserLogin
{
private string constring;
private string sqlcom;
public UserLogin(string a,string b)
{
constring a;
sqlcom b;
}
public int loginexe()
{
OleDbDataReader dr;
String connectionString=constring;
using(OleDbConnection connection=new OleDbConnection(connectionString))
{
OleDbCommand com=new OleDbCommand(sqlcom);
com.Connection=connection;
try
{
connection.Open();
dr=com.ExecuteReader();
bool meet=true;
for(int i=0;i<dr.FieldCount;i++)
{
if(dr.Read())
{
meet=false;
}
}
if(meet==false)
return 1;
else
return 0;
}
catch(Exception ex)
{
return-1;
}
}
}
}
}
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows Form Designer generated code
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
int check=0;
UserLogin ul=new UserLogin(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=example.mdb",@"SELECT Name from customer WHERE Name='" + textBox1.Text + "' ");
check=ul.loginexe();
if(check==1)
label1.Text= "Record Found!";
else if(check==-0)
label1.Text="Record Not Found!";
else
label1.Text="Error on Connection!";
}
}
} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-8-2006 10:39 PM
|
显示全部楼层
原帖由 lieweffect 于 1-8-2006 10:25 PM 发表
check了还是酱
我的code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
...
你用SOMOK东东写??VISUAL STUDIO吗??
可以告诉我ERROR在那一行啊?? |
|
|
|
|
|
|
|
|
|
|
发表于 2-8-2006 05:23 PM
|
显示全部楼层
是啊visual studio.net
最后一行,那个}红色underline,试过加多一个和减一个 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 2-8-2006 05:31 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 2-8-2006 06:00 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 2-8-2006 07:02 PM
|
显示全部楼层
原帖由 lieweffect 于 2-8-2006 06:00 PM 发表
原来如此。。。谢谢你的提醒
你弄了跟我说吧。。。。。
希望可以拉。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 13-9-2008 03:49 PM
|
显示全部楼层
呵呵!!谢!!你2年前的教学。。成功了
example.mdb 应该放在那个folder?? (小弟乃初学者)
之前一直Error on Connection!。。。之后每个folder放一个example.mdb
example.mdb默认的路径,在那的? |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|