C#连接oracle数据库的问题
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.OracleClient;
namespace abc
{
public partial class login_win : Form
{
public login_win()
{
InitializeComponent();
}
private void login_button_Click(object sender, EventArgs e)
{
string connString = "Data Source = nes;User ID = nes ;Password = nes";
string user_name = user_textBox.Text;
string user_password = pw_textBox.Text;
OracleConnection conn = new OracleConnection(connString);
string v_sql = "select * from nes where v_name= '" + user_name + " 'and v_password = '" + user_password + " '";
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = v_sql;
try
{
if (cmd.ExecuteNonQuery() == 0)
{
MessageBox.Show("您的用户名或密码不正确! ");
}
else
{
MessageBox.Show("欢迎使用! ");
}
}
catch
{
MessageBox.Show( "数据库没链接上吗? ");
}
finally
{
conn.Close();
}
}
private void cancel_button_Click(object sender, EventArgs e)
{
Application.ExitThread();
}
}
}
总是提示连接不上 请各位帮忙看看谢谢啊! 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.OracleClient;
namespace abc
{
public partial class login_win : Form
{
public login_win()
{
InitializeComponent();
}
private void login_button_Click(object sender, EventArgs e)
{
string connString = "Data Source = nes;User ID = nes ;Password = nes";
string user_name = user_textBox.Text;
string user_password = pw_textBox.Text;
OracleConnection conn = new OracleConnection(connString);
string v_sql = "select * from nes where v_name= '" + user_name + " 'and v_password = '" + user_password + " '";
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = v_sql;
try
{
if (cmd.ExecuteNonQuery() == 0)
{
MessageBox.Show("您的用户名或密码不正确! ");
}
else
{
MessageBox.Show("欢迎使用! ");
}
}
catch
{
MessageBox.Show( "数据库没链接上吗? ");
}
finally
{
conn.Close();
}
}
private void cancel_button_Click(object sender, EventArgs e)
{
Application.ExitThread();
}
}
}
总是提示连接不上 请各位帮忙看看谢谢啊! 展开
1个回答
展开全部
程序这么写应该没问题,可能问题出在你的连接串配置上,那个名字nes要和你本地oracle\ora92\network\admin\tnsnames.ora中配的例程是不是nes,
分两步判,先判连接,后执行。
private void tbConnectTest_Click(object sender, EventArgs e)
{
string connectionString = tbConnectStr.Text;
if (connectionString == string.Empty)
{
MessageBox.Show("连接字符串不能为空");
}
else
{
try
{
OracleConnection connection = new OracleConnection(connectionString);
connection.Open();
connection.Close();
MessageBox.Show("数据库连接成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void btSqlExecute_Click(object sender, EventArgs e)
{
string connectionString = tbConnectStr.Text;
string commandText = tbSqlStr.Text;
if (commandText == string.Empty)
{
MessageBox.Show("Sql查询不能为空");
}
else
{
try
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
OracleCommand command = new OracleCommand();
command.CommandText = commandText;
command.CommandType = CommandType.Text;
command.CommandTimeout = 30;
command.Connection = connection;
int val = command.ExecuteNonQuery();
MessageBox.Show("Sql执行成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
分两步判,先判连接,后执行。
private void tbConnectTest_Click(object sender, EventArgs e)
{
string connectionString = tbConnectStr.Text;
if (connectionString == string.Empty)
{
MessageBox.Show("连接字符串不能为空");
}
else
{
try
{
OracleConnection connection = new OracleConnection(connectionString);
connection.Open();
connection.Close();
MessageBox.Show("数据库连接成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void btSqlExecute_Click(object sender, EventArgs e)
{
string connectionString = tbConnectStr.Text;
string commandText = tbSqlStr.Text;
if (commandText == string.Empty)
{
MessageBox.Show("Sql查询不能为空");
}
else
{
try
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
OracleCommand command = new OracleCommand();
command.CommandText = commandText;
command.CommandType = CommandType.Text;
command.CommandTimeout = 30;
command.Connection = connection;
int val = command.ExecuteNonQuery();
MessageBox.Show("Sql执行成功");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
网易云信
2023-12-06 广告
2023-12-06 广告
UIkit是一套轻量级、模块化且易于使用的开源UI组件库,由YOOtheme团队开发。它提供了丰富的界面元素,包括按钮、表单、表格、对话框、滑块、下拉菜单、选项卡等等,适用于各种类型的网站和应用程序。UIkit还支持响应式设计,可以根据不同...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询