Code using wrong namespace - c#

I having an aspx.cs file that I am adding code to. I have all the right namespaces and references in my solution, but my code is referencing the wrong namespace with the following error on my Server server = new Server()
System.Web.UI is a 'property' but used as a 'type'
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace IISLoggingSolution
{
public partial class Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//connection string for SQL DB
string connectionString = #"Data Source=Computer\SQLEXPRESS;Initial Catalog=IISLogs;Integrated Security=True";
//Location of the query to be ran
string script = File.ReadAllText(#"C:\\Users\\User\\Desktop\\query.txt");
SqlConnection conn = new SqlConnection(connectionString);
//this is the error line
Server server = new Server(new ServerConnection(conn));
//executes query
server.ConnectionContext.ExecuteNonQuery(script);
}
}
}
How can I get it to use the Microsoft.SqlServer.Management.Smo instead of trying to used the System.Web.UI?

using NS_Server = Microsoft.SqlServer.Management.Common;
using System.Web.UI;
...
NS_Server.Server server = new NS_Server.Server();
the line using NS_Server is an alias. You can use it as a shortcut when there are namespace collisions.

You can use an alias directive:
using Server = Microsoft.SqlServer.Management...Server;
It may not work if property takes precedence, but in that cas you can simply specify the full namespade path.
var server = new Microsoft.SqlServer.Management....Server();

Related

Can't connect to Oracle database in Visual Studio 2019 C#

I'm trying to connect to oracle database but I get this error
System.BadImageFormatException: 'Could not load file or assembly 'Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format.'
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 Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
string ordb = "Data Source =orcl ;User Id = hr ; password =hr;";
OracleConnection conn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
conn = new OracleConnection(ordb);
conn.Open();
}
}
}
Check your dll references, your app seems to be an X64 base, but maybe one of the dll's is of a different format

How do I use C# queries on a prexisting database(SQLite database)?

I have run into a problem where my C# code is creating a whole new database instead of using a preexisting one. Then my program runs into errors where the program cannot find the table to insert the information even though the preexisting database has the table because the code itself is looking at the new table. Here is my 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 Finisar.SQLite;
namespace WestSlope
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
//SQLiteDataAdapter sqlite_datareader;
sqlite_conn = new SQLiteConnection("DataSource=ClientLogDB.db;Version=3;New=True;Compress=True;");
//open conection
sqlite_conn.Open();
//create sql commands
sqlite_cmd = sqlite_conn.CreateCommand();
//Let SQLite command know query is known
sqlite_cmd.CommandText = "INSERT INTO ASAM (ASAMone, ASAMtwo, ASAMthree, ASAMfour, ASAMLim, ASAMLimEX) VALUES ('Had to call', 'Reffered', 'Had to call', 'Watched', 1 , 'Injured legs');"
;
//execute query
sqlite_cmd.ExecuteNonQuery();
sqlite_conn.Close();
}
}
}
What the code is supposed to do is when the user presses a button the program will save information to the preexisting database; but, as you can see the program is making a new database instead of using the preexisting one.
Use new=false in your connection string to use existing database file.
Following should be the connection string:
sqlite_conn = new SQLiteConnection("DataSource=ClientLogDB.db;Version=3;New=False;Compress=True;");

Why am I getting a CS1502 (invalid arguments) error from this code?

I'm facing this issue and I don't know the reason behind it. Here's the error Visual Studio is reporting:
The best overloaded method match for 'PostForum.INSERTforum(int, string, string, System.DateTime)' has some invalid arguments
I'm using Oracle to store my data and also created a procedure called INSERTFORUM. I am not sure if I have a problem with the stored procedure or something else. Please help me sort out this issue.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Forum : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string course_Id = DropDownList1.Text;
int ccourse_Id = Convert.ToInt32(course_Id);
string question = TextBox1.Text;
string posterName = TextBox2.Text;
DateTime blog_date = DateTime.Now;
PostForum.INSERTforum(course_Id, question, posterName, blog_date);
}
}
Code for :Postforum.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public class PostForum
{
public static int INSERTforum(int course_Id, string question, string posterName, DateTime blog_date)
{
int rowsAffected = 0;
using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
{
SqlCommand command = new SqlCommand("INSERTforum", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("#course_Id", SqlDbType.Int).Value = course_Id;
command.Parameters.Add("#question", SqlDbType.VarChar).Value = question;
command.Parameters.Add("#posterName", SqlDbType.VarChar).Value = posterName;
command.Parameters.Add("#blog_date", SqlDbType.DateTime).Value = blog_date;
rowsAffected = command.ExecuteNonQuery();
}
return rowsAffected;
}
}
INSERTforum expects an int as the first parameter. You're passing it a string. Correct your invocation of INSERTforum to read:
PostForum.INSERTforum(ccourse_Id, question, posterName, blog_date);
Note that this will fail if DropDownList1.Text can't be parsed as an integer.
Don't forget to look at the Error List tab in Visual Studio. The error you saw was only the first - the next error would have given you the information I did.

Can't able to connect to Database

I have installed the MySQL server using XAMMP.Created a new database with some data using phpmyadmin.Then i tried to connect to database using this code.But it did not connect.It shows me an error.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Testing!");
SqlConnection myco = new SqlConnection("server=localhost;User Id=root;database=customer;Trusted_Connection=yes");
try
{
myco.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.ReadKey();
}
}
}
Error:
http://pastebin.com/MyEtk4w6
You have MySql then you need to use the classes for MySql not the ones used for SqlServer
MySqlConnection myco = new MySqlConnection("Server=localhost;Database=customer;" +
"Uid=username;Pwd=password;");
Also the connection string for MySql is different
Server=localhost;Database=customer;Uid=username;Pwd=password;
The connection strings for MySql could be very numerous, you should check the correct one for your requirement here at connectionstrings.com
It's because you're using a SqlConnection object which is for Microsoft SQL Server.
Use a MySQLConnection instead.

Connection failed with MySQL .NET Connector

I cannot connect to MySQL it fails on line connection.Open(), is there something wrong with my code ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace MySQLConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost:3316;" +
"DATABASE=mydb;" +
"UID=user;" +
"PASSWORD=password;";
MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
// ...
connection.Close();
}
}
}
this is the string format I use to connect via the MySql.Data.dll version 6.1.2.0
server={0};user id={1};password={2};database={3};port={4}
so your connection string should be
server=localhost;user id=user;password=password;database=mydb;port=3316
You need to specify the Port as a separate argument in the connection string and it looks like the password key is "Pwd" instead of "Password".
See connectionstrings.com for help on the exact syntax.

Categories

Resources