使用SQL语句创建学生成绩数据库db_Stu,该数据库有一个主数据库文件(’F:\ db_Stu.mdf')和事务日志(’F:
发布网友
发布时间:2022-05-09 17:36
我来回答
共3个回答
热心网友
时间:2023-10-10 19:33
有个问题 刚好符合你的要求:
问题:
当前目录下的Example.mdb数据库(这个是Access数据库)中,内含一个数据表student,有三个字段:学号、姓名、密码,并有如干记录。连接串为string connStr="provider=microsoft.jet.oledb.4.0;datasource="+Server.MapPath("Example.mdb"),数据库对象不是SQL,而是Access,采用OleDb,即将对象前SQL换成OleDb。
1、编写一段程序,将student中的数据在DataGridl中显示出来。
2、编写一段程序,将学号、姓名、密码分别为0001,张山,abcd的学生记录插入到student中。
3、编写一段程序,判断数据库中是否存在学号为“01”、口令为“1234”的学生。
首先连接数据库(这里的链接字符串是在配置文件中)
static string connectionString = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
private OleDbConnection connection;
public OleDbConnection Connection {
get{
if (connection == null){
connection = new OleDbConnection(connectionString);}
else if (connection.State == System.Data.ConnectionState.Closed) //关闭
{
connection.Open();}
else if (connection.State == System.Data.ConnectionState.Broken) //中断
{connection.Close();
connection.Open();}
return connection;
}
}
然后是执行查询语句和插入语句的方法;
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString, params OleDbParameter[] cmdParms)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.OleDb.OleDbException E)
{
throw new Exception(E.Message);
}
}
}
}
/// <summary>
/// 执行查询语句,返回OleDbDataReader
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>OleDbDataReader</returns>
public static OleDbDataReader ExecuteReader(string strSQL)
{
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(strSQL, connection);
try
{
connection.Open();
OleDbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.OleDb.OleDbException e)
{
throw new Exception(e.Message);
}
}
下面是查询方法和插入方法
(由于时间问题,你把数据库字段都换成你自己的,我这里就不换了)
/// <summary>
/// 增加一条数据
/// </summary>
public int Add()
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into [student](");
strSql.Append("[stNum],[Name],[Psd])");
strSql.Append(" values (");
strSql.Append("@stNum,@Name,@Psd)");
OleDbParameter[] parameters = {
new OleDbParameter("@stNum", OleDbType.VarChar,50),
new OleDbParameter("@Name", OleDbType.VarChar),
new OleDbParameter("@Psd", OleDbType.VarChar,50)};
parameters[0].Value = "0001";
parameters[1].Value = "张山";
parameters[2].Value = “abcd”;
int num=ExecuteCommand(strSql.ToString(),parameters);
return num;
}
/// <summary>
/// 得到所有对象
/// </summary>
/// <returns></returns>
public IList<Student> GetAll()
{
IList<Student> modelList = new List<Student>();
string sqlStr = "select * from [student] ORDER BY [Id] DESC";
using (OleDbDataReader reader = DbHelperOleDb.ExecuteReader(sqlStr))
{
while (reader.Read())
{
Student model = new Student();
if (reader["Id"].ToString() != "")
{
model.Id = int.Parse(reader["Id"].ToString());
}
model.stNum = reader["stNum"].ToString();
model.Name = reader["Name"].ToString();
model.Psd = reader["Psd"].ToString();
modelList.Add(model);
}
}
return modelList;
}
页面调用添加方法就可以添加一条记录了
下面市页面绑定数据
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindGV();
}
}
public void DataBindGV()
{
//GetAll()方法我是放在Student类里面的
gvStudentList.DataSource = new sutdent().GetAll();
gvStudentList.DataBind();
}
页面DataGrid的代码如下
<asp:GridView ID="gvStudentList" runat="server" BorderWidth="0px" BackColor="#999999"
CellPadding="1" CellSpacing="1" Width="100%"
AutoGenerateColumns="False">
<EmptyDataTemplate>
没有资料!
</EmptyDataTemplate>
<HeaderStyle Height="20px" BackColor="#CCCCCC" />
<FooterStyle />
<RowStyle BackColor="#fafafa" Height="20px" />
<EmptyDataRowStyle BackColor="#ffffff" />
<SelectedRowStyle />
<Columns>
<asp:BoundField DataField="Title" HeaderText="学号" SortExpression="stNum">
<ItemStyle Width="170px" />
</asp:BoundField>
<asp:TemplateField HeaderText="名称" SortExpression="Name">
<ItemTemplate>
<%#Eval("Name")) %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="密码" SortExpression="Psd">
<ItemTemplate>
<%#Eval("Psd") %>
</ItemTemplate>
<ItemStyle Width="90px" />
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#eeeeee" BorderWidth="0" />
</asp:GridView>
关于第3个,只需把GetAll()方法的查询语句换一下就行了,换成如下语句:
select * from [student] where [stNum]='01' and [Psd]='1234'
然后再查询,根据查询的结果List判断是否存在,当然你也可以执行
select count(1) from [student] where [stNum]='01' and [Psd]='1234'
判断返回的数是否大于零就行了,当然这个查询要另外的查询语句才行用上面的查询方法不行了,需要执行ExecuteScalar()了,自己弄吧,很简单的
如此你的问题全都解决了
热心网友
时间:2023-10-10 19:34
create database student
on primary
(
name='student_data',
filename='f:\db_Stu.mdf',
size=5mb,
maxsize=100mb,
filegrowth=15%
)
log on
(
name='student_log',
filename=''f:\db_Stu_log.ldf'
)
go
热心网友
时间:2023-10-10 19:34
create database db_stu
on
(
filename='F:\ db_Stu.mdf',
size=8M
)
log on
(
filename='F:\ db_Stu.log',
size=50M
)