你喜欢sqlite中的错误。请快速回复 [英] hi error in sqlite . please give reply fast
本文介绍了你喜欢sqlite中的错误。请快速回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码背后的代码
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 System.Data.SQLite;
使用 System.Data.SqlClient;
namespace usesqlitecsharp
{
public partial class Form1:Form
{
public Form1()
{
Data_connection dbobject = new Data_connection();
SQLiteConnection SQLconnect = new SQLiteConnection();
InitializeComponent();
}
private void Form1_Load( object sender,EventArgs e)
{
SQLconnect.ConnectionString = dbobject.datalocation();
SQLconnect.Open();
}
private void button1_Click( object sender,System.EventArgs e)
{
SQLiteCommand SQLcommand = new SQLiteCommand();
SQLcommand = SQLconnect.CreateCommand();
SQLcommand.CommandText = CREATE TABLE IF NOT NOT EXISTS + textBox1.Text + (用户名TEXT,密码TEXT);;
SQLcommand.ExecuteNonQuery();
SQLcommand.Dispose();
MessageBox.Show( 表创建);
}
private void button2_Click( object sender,EventArgs e)
{
SQLiteCommand cmd = new SQLiteCommand();
cmd = SQLconnect.CreateCommand();
cmd.CommandText = 插入 + textBox1.Text + (用户名,密码)值(@ username,@ password);
cmd.Parameters.AddWithValue( @ username,textBox2.Text);
cmd.Parameters.AddWithValue( @ password,textBox3.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
MessageBox.Show( 数据已插入);
}
private void button3_Click( object sender,EventArgs e)
{
SQLiteCommand cmd = new SQLiteCommand( select * from + textBox1.Text,SQLconnect);
SQLiteDataAdapter da = new SQLiteDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
if (dt.Rows.Count > 0 )
{
dataGridView1.DataSource = dt;
}
else
{
MessageBox.Show( 表中没有数据存在);
}
}
}
}
dotaconnection.cs代码是
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 System.Data.SQLite;
使用 System.Data.SqlClient;
namespace usesqlitecsharp
{
public partial class Form1:Form
{
public Form1()
{
Data_connection dbobject = new Data_connection();
SQLiteConnection SQLconnect = new SQLiteConnection();
InitializeComponent();
}
private void Form1_Load( object sender,EventArgs e)
{
SQLconnect.ConnectionString = dbobject.datalocation();
SQLconnect.Open();
}
private void button1_Click( object sender,System.EventArgs e)
{
SQLiteCommand SQLcommand = new SQLiteCommand();
SQLcommand = SQLconnect.CreateCommand();
SQLcommand.CommandText = CREATE TABLE IF NOT NOT EXISTS + textBox1.Text + (用户名TEXT,密码TEXT);;
SQLcommand.ExecuteNonQuery();
SQLcommand.Dispose();
MessageBox.Show( 表创建);
}
private void button2_Click( object sender,EventArgs e)
{
SQLiteCommand cmd = new SQLiteCommand();
cmd = SQLconnect.CreateCommand();
cmd.CommandText = 插入 + textBox1.Text + (用户名,密码)值(@ username,@ password);
cmd.Parameters.AddWithValue( @ username,textBox2.Text);
cmd.Parameters.AddWithValue( @ password,textBox3.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
MessageBox.Show( 数据已插入);
}
private void button3_Click( object sender,EventArgs e)
{
SQLiteCommand cmd = new SQLiteCommand( select * from + textBox1.Text,SQLconnect);
SQLiteDataAdapter da = new SQLiteDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
if (dt.Rows.Count > 0 )
{
dataGridView1.DataSource = dt;
}
else
{
MessageBox.Show( 表中没有数据存在);
}
}
}
}
错误是
错误5当前上下文中不存在名称SQLconnectc :\users\srikanth \documents\visual studio 2012 \Projects \usesqlitecsharp\usesqlitecsharp\Form1.cs 33 26 usesqlitecsharp
解决方案
Quote:public Form1()
{
Data_connection dbobject = new Data_connection();
SQLiteConnection SQLconnect = new SQLiteConnection();
InitializeComponent();
}
在上面的代码中,您创建了一个SQLiteConnection对象,该对象是代码的其他部分不可用 垃圾收集的候选者
您应该将SQLconnect
声明为类成员。
在两个构造函数中,你使用局部变量
SQLiteConnection SQLconnect = new SQLiteConnection();
构造函数完成后超出范围。你必须创建类成员变量:
public partial class Form1:Form
{
SQLiteConnection _SQLconnect;
public Form1()
{
Data_connection dbobject = new Data_connection();
// 现在实例化表单中每个方法可用的变量
_SQLconnect = new SQLiteConnection();
InitializeComponent();
}
}
尝试将其作为类成员声明远离consturctor,然后你可以在构造函数中初始化它如下
public 部分 类 Form1:表单
{
SQLiteConnection SQLconnect;
public Form1()
{
Data_connection dbobject = new Data_connection ();
SQLconnect = new SQLiteConnection();
InitializeComponent();
}
所以你可以在该分类的任何级别访问它
my code behind code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Data.SqlClient;
namespace usesqlitecsharp
{
public partial class Form1 : Form
{
public Form1()
{
Data_connection dbobject = new Data_connection();
SQLiteConnection SQLconnect = new SQLiteConnection();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SQLconnect.ConnectionString = dbobject.datalocation();
SQLconnect.Open();
}
private void button1_Click(object sender, System.EventArgs e)
{
SQLiteCommand SQLcommand = new SQLiteCommand();
SQLcommand = SQLconnect.CreateCommand();
SQLcommand.CommandText = "CREATE TABLE IF NOT EXISTS " + textBox1.Text + "( Username TEXT, Password TEXT);";
SQLcommand.ExecuteNonQuery();
SQLcommand.Dispose();
MessageBox.Show("Table Created");
}
private void button2_Click(object sender, EventArgs e)
{
SQLiteCommand cmd = new SQLiteCommand();
cmd = SQLconnect.CreateCommand();
cmd.CommandText = "insert into " + textBox1.Text + " (Username,Password) values (@username,@password)";
cmd.Parameters.AddWithValue("@username", textBox2.Text);
cmd.Parameters.AddWithValue("@password", textBox3.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
MessageBox.Show("Data Inserted");
}
private void button3_Click(object sender, EventArgs e)
{
SQLiteCommand cmd = new SQLiteCommand("select * from " + textBox1.Text, SQLconnect);
SQLiteDataAdapter da = new SQLiteDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
if (dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
}
else
{
MessageBox.Show("No Data Exist in Table");
}
}
}
}
dotaconnection.cs code is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Data.SqlClient;
namespace usesqlitecsharp
{
public partial class Form1 : Form
{
public Form1()
{
Data_connection dbobject = new Data_connection();
SQLiteConnection SQLconnect = new SQLiteConnection();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SQLconnect.ConnectionString = dbobject.datalocation();
SQLconnect.Open();
}
private void button1_Click(object sender, System.EventArgs e)
{
SQLiteCommand SQLcommand = new SQLiteCommand();
SQLcommand = SQLconnect.CreateCommand();
SQLcommand.CommandText = "CREATE TABLE IF NOT EXISTS " + textBox1.Text + "( Username TEXT, Password TEXT);";
SQLcommand.ExecuteNonQuery();
SQLcommand.Dispose();
MessageBox.Show("Table Created");
}
private void button2_Click(object sender, EventArgs e)
{
SQLiteCommand cmd = new SQLiteCommand();
cmd = SQLconnect.CreateCommand();
cmd.CommandText = "insert into " + textBox1.Text + " (Username,Password) values (@username,@password)";
cmd.Parameters.AddWithValue("@username", textBox2.Text);
cmd.Parameters.AddWithValue("@password", textBox3.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
MessageBox.Show("Data Inserted");
}
private void button3_Click(object sender, EventArgs e)
{
SQLiteCommand cmd = new SQLiteCommand("select * from " + textBox1.Text, SQLconnect);
SQLiteDataAdapter da = new SQLiteDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
if (dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
}
else
{
MessageBox.Show("No Data Exist in Table");
}
}
}
}
error is
Error 5 The name 'SQLconnect' does not exist in the current context c:\users\srikanth\documents\visual studio 2012\Projects\usesqlitecsharp\usesqlitecsharp\Form1.cs 33 26 usesqlitecsharp
解决方案
Quote:public Form1()
{
Data_connection dbobject = new Data_connection();
SQLiteConnection SQLconnect = new SQLiteConnection();
InitializeComponent();
}
In the above code you create a SQLiteConnection object that is
- Unavailable in other parts of your code
- Candidate to garbage collection
You should declareSQLconnect
as class member.
In both constructors you're using local variable
SQLiteConnection SQLconnect = new SQLiteConnection();
which goes out of scope after the constructor finishes. You have to create class member variable:
public partial class Form1 : Form { SQLiteConnection _SQLconnect; public Form1() { Data_connection dbobject = new Data_connection(); // Now instantiate the variable that will be available to every method within your form _SQLconnect = new SQLiteConnection(); InitializeComponent(); } }
try to declare it away from the consturctor as a class member , then you can initialize it in the constructor as follows
public partial class Form1 : Form { SQLiteConnection SQLconnect; public Form1() { Data_connection dbobject = new Data_connection(); SQLconnect = new SQLiteConnection(); InitializeComponent(); }
so you can have access to it at any level in that classd
这篇关于你喜欢sqlite中的错误。请快速回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文