如何通过MVC向数据库中添加数据用 的是。net、C#、linq to sql 类,在线等!!!

如何通过MVC向数据库中添加数据用 的是。net、C#、linq to sql 类,在线等!!!,第1张

上学期教同学时写了一个绝对适合入门的小例子 用的是aspnet mvc3+linq to entity

linq to entity和linq to sql在使用上本身差距不大 你肯定能看懂的

razor页面中用的是弱类型的helper

还有疑问可以去此论坛net区提问:

>

package comkiwworqqbeans;

import javasqlConnection;

import javasqlDriverManager;

import javasqlResultSet;

import javasqlSQLException;

import javasqlStatement;

/

数据库 *** 作的JavaBean类,用于对数据库的查询与更新的实现;

该类默认的连接的数据库为本地数据库,连接数据库为"qq";

该类主要为用户一系列的数据 *** 作提供底层服务。

@author kiwwor

@see UserBean

/

public class Access {

//驱动程序类

private String driver = "commicrosoftsqlserverjdbcSQLServerDriver";

//连接数据库url

private String connectionUrl="jdbc:sqlserver://localhost:1433;DatabaseName=qq";

//用户名

private String user = "qq";

//用户密码

private String password = "123258741";

//数据库连接对象

private Connection connection = null;

//数据库对象

private Statement statement = null;

//数据集对象

private ResultSet resultSet = null;

public String getDriver() {

return driver;

}

public void setDriver(String driver) {

thisdriver = driver;

}

public String getConnectionUrl() {

return connectionUrl;

}

public void setConnectionUrl(String connectionUrl) {

thisconnectionUrl = connectionUrl;

}

public String getUser() {

return user;

}

public void setUser(String user) {

thisuser = user;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

thispassword = password;

}

public Connection getConnection() {

return connection;

}

public void setConnection(Connection connection) {

thisconnection = connection;

}

public Statement getStatement() {

return statement;

}

public void setStatement(Statement statement) {

thisstatement = statement;

}

public ResultSet getResultSet() {

return resultSet;

}

public void setResultSet(ResultSet resultSet) {

thisresultSet = resultSet;

}

/

获取一个连接对象,默认连接对象本地数据库qq。

@return 连接是否成功

/

public boolean createConnection() {

boolean b = false;

try {

ClassforName(driver);

connection = DriverManagergetConnection(connectionUrl, user, password);

b = true;

} catch (Exception e) {

eprintStackTrace();

}

return b;

}

/

更新数据库

@param sql 更新的sql语句

@return 更新是否成功

/

public boolean update(String sql) {

boolean b =false;

try {

statement = connectioncreateStatement();

statementexecute(sql);

b = true;

} catch (Exception e) {

eprintStackTrace();

}

return b;

}

/

执行查询,将查询的结果集给resultmentSet。

@param sql 查询的sql语句

/

public void query(String sql) {

try {

statement = connectioncreateStatement();

resultSet = statementexecuteQuery(sql);

} catch (Exception e) {

eprintStackTrace();

}

}

/

检测结果集是否为空

@return true为存在记录

/

public boolean next() {

boolean b = false;

try {

if (resultSetnext()) b = true;

} catch (Exception e) {

eprintStackTrace();

}

return b;

}

/

获得结果集中当前行columnLabel的记录

@param columnLabel

@return 值记录

/

public String getValue(String columnLabel) {

String value = null;

try {

if (resultSet != null) value = resultSetgetString(columnLabel);

} catch (Exception e) {

eprintStackTrace();

}

return value;

}

/

关闭连接对象

/

public void closeConnection() {

try {

if (connection != null) connectionclose();

} catch (SQLException e) {

eprintStackTrace();

}

}

/

关闭数据库对象

/

public void closeStatement() {

try {

if (statement != null) statementclose();

} catch (SQLException e) {

eprintStackTrace();

}

}

/

关闭结果集

/

public void closeResultSet() {

try {

if (resultSet != null) resultSetclose();

} catch (SQLException e) {

eprintStackTrace();

}

}

/

关闭数据连接对象,数据库对象和数据结果集对象。

/

public void closeAll() {

closeResultSet();

closeStatement();

closeConnection();

}

/

测试该类函数。

@param args

/

public static void main(String[] args) {

Access db = new Access();

if (dbcreateConnection()) {

Systemoutprintln("测试数据库连接成功");

String sql = "select id from [user]";

dbquery(sql);

int i = 1;

while (dbnext()) {

String s = dbgetValue("id");

Systemoutprintln("查询用户" + i + ": " + s);

i++;

}

dbcloseResultSet();

dbcloseStatement();

dbcloseConnection();

}

}

}

第二个:

package comkiwworqqbeans;

/

这是一个对用户的数据查询和更新的JavaBean类;

该类提供了对用户的密码认证,用户注册等方法。

@author kiwwor

@see Access

/

public class UserBean {

/

对用户的登录认证

@param id 用户名

@param password 密码

@return 认证结果

/

public boolean isValid(String id, String password) {

boolean b = false;

Access db =new Access();

if (dbcreateConnection()) {

String sql = "select from [user] where id=" + id +"and password=" + password;

dbquery(sql);

if (dbnext()) {

b = true;

}

}

dbcloseAll();

return b;

}

/

用来查询该用户在服务器中是否存在

@param id 用户名

@return 确认是否存在

/

public boolean isExit(String id) {

boolean b = false;

Access db =new Access();

if (dbcreateConnection()) {

String sql = "select from [user] where id=" + id;

dbquery(sql);

if (dbnext()) {

b = true;

}

}

dbcloseAll();

return b;

}

/

添加用户,该方法必须在<code>isExit(String id)</code>确认的前提下使用,否则可能出现异常

@param id 新增用户名

@param password 密码

@return 确认添加是否成功

/

public boolean add(String id, String password) {

boolean b = false;

Access db =new Access();

if (dbcreateConnection()) {

String sql = "insert into [user] (id, password) values (" + id + "," + password + ")";

b = dbupdate(sql);

}

dbcloseAll();

return b;

}

}

以上就是关于如何通过MVC向数据库中添加数据用 的是。net、C#、linq to sql 类,在线等!!!全部的内容,包括:如何通过MVC向数据库中添加数据用 的是。net、C#、linq to sql 类,在线等!!!、vs2013中mvc里面的数据库服务怎么添加、MVC中,Controller如何从数据库调用单个数据给一个变量等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10191078.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存