i have compile the application to Setup.exe with Install Sheild in Visual studio 2015 with SQL server 2012. The application work on my computer but when i transfer Setup.exe it to clients computer it didnt work generate problem with client computer.
Need A way to handle the application with
I have attached the database to SQLSERVER Managment studio.
I have
change configuration File Data Source=""to client computer Address
but still not working.
public static string GetConnection()
{
return ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
}
SqlConnection con = new SqlConnection(GetConnection());
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("Select EmpID from RegForm",con);
int i= Convert.ToInt32(cmd.ExecuteScalar());
if(i!=0)
{
MessageBox.Show("value type is " + i);
con.Close();
MessageBox.Show("Connection is closed ");
}
}
I just want that this compile application Start working with setting path in configuration DataSource Attribute nd the programming start working with Active Database
Related
I'm working on a small project that's unrelated to this, it needs a database to be integrated into it, so I looked into SQLite and tried to teach myself the basics.
My code works fine but I realised if I was to deploy this on another machine it wouldn't be able to connect to the database because it wouldn't have the file as I've hard coded it in (it's running of my C:\ drive currently which will be an issue for users who haven't got it there obviously).
I was wondering if it was possible to update to connection string for the user at runtime? Even say, when the user installs it, it installs a copy of the database and changes its path to that?
Here's my code:
using System;
using System.Windows.Forms;
using System.Data.SQLite;
namespace sqltest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string cs = #"Data Source=c:\student.db;Version=3;FailIfMissing=False";
string stm = "SELECT Name FROM Customer";
using var con = new SQLiteConnection(cs);
con.Open();
using var cmd = new SQLiteCommand(stm, con);
using SQLiteDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
MessageBox.Show($"{rdr.GetString(0)}");
}
}
}
}
Thanks for any help!
you can get datasource from config file, e.g. appsetting.json
The connection string that you pass to new SQLiteConnection(...); is actually being passed at runtime already.
Sounds like the simplest solution is to create the database if it doesn't already exists with a predetermined path. This will ensure that when your script runs on a machine that doesn't have a DB, the DB will be created at runtime.
Here's a relevant post: Programmatically create sqlite db if it doesn't exist?
I have create a simple login application using the C#.net.
I have created database test in which I have created table called as login.
Table: Login contains:
create table login
(
name varchar(20),
pass varchar(20)
)
Here is the login button code which I have written in the C#.net:
private void BtnLogin_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=ServerName;Initial Catalog=test;Integrated Security=True";
con.Open();
SqlDataReader dr = null;
SqlCommand cmd = new SqlCommand("Select * from login", con);
dr = cmd.ExecuteReader();
int count = 0;
while (dr.Read())
{
if (textBox1.Text == dr[0].ToString() && textBox2.Text == dr[1].ToString())
{
count = count + 1;
}
else
{
count = count + 0;
}
}
if (count == 1)
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show("Fail");
}
}
Note: The above example works fine for me if I installed the Visual Studio 2010 and SQL Server Management studio in a single machine.
But
I want to run the above application in the machine where only Visual Studio 2010 is installed not SQL Server Management Studio.
Is it possible?
Data Source=ServerName;Initial Catalog=test;Persist Security Info=True;User ID=YourUserId;Password=YourPassword
Also you have to instal .net framework on your local machine
The SqlClient types that you are using (SqlConnection, SqlDataReader, and so on) are defined in System.Data.dll (you can see this by going to the MSDN docs, the assembly is documented just above the big 'Syntax' heading') which is part of the .NET Framework. So as long as you have .NET Framework installed on a machine, you do not need any additional dependencies such as Visual Studio or SSMS.
I have the trouble with connection to MS SQL Server from Smart Device Project in Visual Studio 2008.
private void button2_Click(object sender, EventArgs e)
{
string connStr = "Data Source=SERVER-5;Initial Catalog=MydB;Integrated Security=SSPI;Connection Timeout=5";
DataTable data;
using (var connection = new SqlConnection(connStr))
{
try
{
var sda = new SqlDataAdapter("select * from pri_date", connection);
var ds = new DataSet();
sda.Fill(ds);
data = ds.Tables[0];
}
catch (SqlException ex)
{
var exc = ex.InnerException;
}
}
}
I'm trying to run this code in Visual Studio and I'm getting the error "Specified SQL server not found: SERVER-5" .
When I tried to launch this code in Windows application everything worked ok.
It's highly likely that it's a name resolution issue. If you use the server's IP address instead of the name, it will likely work. If that does work, then you need to make sure you have the DNS Server properly set for your device's network adapter.
I have two functions. Insert functions provided by Soner Gönül (thanks),......
Table Name Students
Database
`Field Name Data Type
*StudentID Number
StudentName Text
StudentCNIC Text
StudentDOB Date/Time
*PK
using System.Data.OleDb;
private void Form1_Load(object sender, EventArgs e)
{
myCon = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Access_and_CSharp.accdb");
this.studentsTableAdapter.Fill(this.access_and_CSharpDataSet.Students);
}
Insert Function
private void Insertbtn_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Students(StudentID, StudentName, StudentCNIC, StudentDOB) Values(#StudIDTxt, #StudNameTxt, #StudCNCITxt, #StudDOBTxt)";
cmd.Parameters.AddWithValue("#StudIDTxt", StudIDTxt.Text);
cmd.Parameters.AddWithValue("#StudNameTxt", StudNameTxt.Text);
cmd.Parameters.AddWithValue("#StudCNCITxt", StudCNCITxt.Text);
cmd.Parameters.AddWithValue("#StudDOBTxt", StudDOBTxt.Text);
cmd.Connection=myCon;
myCon.Open();
cmd.ExecuteNonQuery();
myCon.Close();
}
And this is the update function
private void Updatebtn_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE [Students] set [StudentName] = ?, [StudentCNIC] = ?, [StudentDOB] = ? WHERE [StudentID] = ?";
cmd.Parameters.AddWithValue("#StudIDTxt", StudIDTxt.Text);
cmd.Parameters.AddWithValue("#StudNameTxt", StudNameTxt.Text);
cmd.Parameters.AddWithValue("#StudCNCITxt", StudCNCITxt.Text);
cmd.Parameters.AddWithValue("#StudDOBTxt", StudDOBTxt.Text);
cmd.Connection = myCon;
myCon.Open();
int rowsAffected = cmd.ExecuteNonQuery();
myCon.Close();
}
Problem 1 -
When I use the insert function I can see new data at the front end. But I cannot see the new data in Access. Other times when I close the application and restart, new recorded is not there. If I look in Access application and closed it then open VS2010 application new data is not there. What is going on?
Problem 2 -
When I use the update function, data remains updated while the application running first time. This is not true when the application is closed and running again. Where have I gone wrong?
For both of problems can anyone see where the problem(s) is/are?
Thanks in advance
EDIT
Updating to say I am looking the following website where I have gone wrong.
http://www.c-sharpcorner.com/uploadfile/e628d9/inserting-retrieving-records-from-ms-access-2007-using-odbc/
Update
I have Windows 7, MS Access 2007 and VS 2010. I am wondering if this is the problem. If it is then it's probably not worth the trouble. I have downloaded AccessDatabaseEngine but its 32bit so I don't know? Probably make my life easier if I use SQL Server instead of Access.
I think this question has been asked too many times.
For starters, make sure your database is NOT included with your project builds.
In Solution Explorer, find your database and specify Build Action = None and Copy to Output Directory = Do not copy
Your database should reside in a folder other than your bin folder, otherwise every time you fire up your project, you will copy to your output folder the same database that is in your project.
Instead, locate the database somewhere else (C:\Program Files (x86)\Common Files or some other location) and connect to it.
There could be other issues with your project, but this is a big one that is jumping out at me.
I need to connect to a Oracle DB (external) through Visual Studio 2010. But I dont want to install Oracle on my machine.
In my project I referenced: System.Data.OracleClient. But its not fulfilling the need.
I have an "Oracle SQL Developer IDE" in which I run SQL queries against oracle db.
I have this code so far:
private static string GetConnectionString()
{
String connString = "host= serverName;database=myDatabase;uid=userName;pwd=passWord";
return connString;
}
private static void ConnectingToOracle()
{
string connectionString = GetConnectionString();
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
Console.WriteLine("State: {0}", connection.State);
Console.WriteLine("ConnectionString: {0}",
connection.ConnectionString);
OracleCommand command = connection.CreateCommand();
string sql = "SELECT * FROM myTableName";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string myField = (string)reader["MYFIELD"];
Console.WriteLine(myField);
}
}
}
So far I read these blogs:
http://st-curriculum.oracle.com/tutorial/DBXETutorial/index.htm
http://blogs.msdn.com/b/kaevans/archive/2009/07/18/connecting-to-oracle-from-visual-studio.aspx
So far I have not downloaded anything from Oracle. What steps should I take to make this happen?
First off you need to download and install ODP from this site
http://www.oracle.com/technetwork/topics/dotnet/index-085163.html
After installation add a reference of the assembly Oracle.DataAccess.dll.
Your are good to go after this.
using System;
using Oracle.DataAccess.Client;
class OraTest
{
OracleConnection con;
void Connect()
{
con = new OracleConnection();
con.ConnectionString = "User Id=<username>;Password=<password>;Data Source=<datasource>";
con.Open();
Console.WriteLine("Connected to Oracle" + con.ServerVersion);
}
void Close()
{
con.Close();
con.Dispose();
}
static void Main()
{
OraTest ot= new OraTest();
ot.Connect();
ot.Close();
}
}
You can use Oracle.ManagedDataAccess NuGet package too (.NET >= 4.0, database >= 10g Release 2).
Using Nuget
Right click Project, select Manage NuGet packages...
Select the Browse tab, search for Oracle and install Oracle.ManagedDataAccess
In code use the following command (Ctrl+. to automatically add the using directive).
Note the different DataSource string which in comparison to Java is
different.
// create connection
OracleConnection con = new OracleConnection();
// create connection string using builder
OracleConnectionStringBuilder ocsb = new OracleConnectionStringBuilder();
ocsb.Password = "autumn117";
ocsb.UserID = "john";
ocsb.DataSource = "database.url:port/databasename";
// connect
con.ConnectionString = ocsb.ConnectionString;
con.Open();
Console.WriteLine("Connection established (" + con.ServerVersion + ")");
The next approach work to me with Visual Studio 2013 Update 4
1- From Solution Explorer right click on References then select add references
2- Assemblies > Framework > System.Data.OracleClient > OK
and after that you free to add using System.Data.OracleClient in your application and deal with database like you do with Sql Server database except changing the prefix from Sql to Oracle as in SqlCommand become OracleCommand for example to link to Oracle XE
OracleConnection oraConnection = new OracleConnection(#"Data Source=XE; User ID=system; Password=*myPass*");
public void Open()
{
if (oraConnection.State != ConnectionState.Open)
{
oraConnection.Open();
}
}
public void Close()
{
if (oraConnection.State == ConnectionState.Open)
{
oraConnection.Close();
}}
and to execute some command like INSERT, UPDATE, or DELETE using stored procedure we can use the following method
public void ExecuteCMD(string storedProcedure, OracleParameter[] param)
{
OracleCommand oraCmd = new OracleCommand();
oraCmd,CommandType = CommandType.StoredProcedure;
oraCmd.CommandText = storedProcedure;
oraCmd.Connection = oraConnection;
if(param!=null)
{
oraCmd.Parameters.AddRange(param);
}
try
{
oraCmd.ExecuteNoneQuery();
}
catch (Exception)
{
MessageBox.Show("Sorry We've got Unknown Error","Connection Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
Basically in this case, System.Data.OracleClient need access to some of the oracle dll which are not part of .Net. Solutions:
Install Oracle Client , and add bin location to Path environment varaible of windows
OR
Copy
oraociicus10.dll (Basic-Lite version) or aociei10.dll (Basic version),
oci.dll, orannzsbb10.dll and oraocci10.dll from oracle client installable folder to bin folder of application so that application is able to find required dll