Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Me and my 'team' from our informatics class have been trying to get a program running and connecting it to a SQL server, it seems we keep getting errors. Could you help me try and figure out the problem, I have been trying to fix this for 3 straight days so any help is welcome.
Code we have so far:
Try
Dim conn As New MySqlConnection
Dim test As String = 64012478
conn.ConnectionString = "User ID=*****_****; Password=******; Initial Catalog=dainformat_idea; Data Source=(IP of website);"
conn.Open()
Dim query = "SELECT * From students"
Dim commando As New MySqlCommand(query, conn)
Dim reader As MySqlDataReader = commando.ExecuteReader
reader.Read()
If reader.HasRows Then
MessageBox.Show(reader(0) & " " & reader(1) & "Kaas")
Exit Sub
Else
MessageBox.Show("Could not find something")
Exit Sub
End If
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try`
You are trying to connect a SQL Server with the MySQL libraries... If you have mySQL on the server the code will run fine, but only if you have mysql, otherwise the connection will fail.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to insert data into an existing Microsoft SQL Server 2014 table using VB.NET 2017. The code does not give me any errors, but it does not insert the record either. Right now I just want to get it to where it alters data, without worrying about user input. Any help is appreciated.
My guess is that it is not connecting to SQL Server properly.
Imports System.Data.SqlClient
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlstr As String
Dim connectionString As String = "server=localhost;database= database1;Trusted_Connection=True;"
sqlstr = "Insert into tblname (id, gender) VALUES ([5], ['Bob'])"
Try
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim cmdInsert As New SqlCommand(sqlstr, connection)
connection.Close()
End Using
MsgBox("anything") 'this is just to see if it even runs
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
After you create the SqlCommand, just call ExecuteNonQuery to run the SQL statement:
Dim cmdInsert As New SqlCommand(sqlstr, connection)
cmdInsert.ExecuteNonQuery()
connection.Close()
But note that the values in your INSERT statement should not be surrounded by brackets:
sqlstr = "Insert into tblname (id, gender) VALUES (5, 'Bob')"
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a login form in VB.NET that requires a username and password input. If the user inputs match the correct login details in the SQL Server DB then another VB form is shown.
The password needs to be hashed, without using MD5.
Dim Bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(txtPassword.Text)
Dim HashofBytes() As Byte = New System.Security.Cryptography.SHA1Managed().ComputeHash(bytes)
Dim StrHash As String = Convert.ToBase64String(HashofBytes)
Using con As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""G:\Program\X\Database1.mdf"";Integrated Security=True")
con.Open()
Dim query As String = "SELECT COUNT(*) FROM Users WHERE Username=#Username AND Password=#Password"
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.Add(New SqlParameter("#Username", txtUsername.Text))
cmd.Parameters.Add(New SqlParameter("#Password", StrHash))
Try
If cmd.ExecuteScalar() = 1 Then
frmOverview.ShowDialog()
Me.Hide()
Else
MsgBox("You have entered an invalid username or password")
End If
Catch ex As SqlException
MsgBox(ex.Message.ToString())
End Try
End Using
txtPassword.Clear()
However, the issue I have is that even if the user inputs the correct login details the next form is not shown. How could this be resolved?
As #Plutonix said, check your Password where clause:
SELECT COUNT(*) FROM Users WHERE Username=#Username AND #Password=#Password
it should be:
...AND Password=#Password
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to update an int field of ODBC linked SQL Server table in my Access DB, from another nvarchar field, with following codes. But it fails to run, I don't know what I missed, appreciate if someone can help. Thanks a lot.
Function update_test()
Dim qryd As QueryDef
Set qryd = CurrentDb.CreateQueryDef("")
qryd.Connect = "ODBC;DSN=ODBC11;"
qryd.SQL = "UPDATE table1 SET int_field = CAST(text_field AS int)"
qryd.ReturnsRecords = False
qryd.Execute
End Function
My ODBC connection is correct and stable, and the following code can be run successfully, that's why I am confused why above codes cannot be run.
These codes can be run succesfully:
Function update_test()
Dim qryd As QueryDef
Set qryd = CurrentDb.CreateQueryDef("")
qryd.Connect = "ODBC;DSN=ODBC11;"
qryd.SQL = "UPDATE table1 SET int_field = other_int_field"
qryd.ReturnsRecords = False
qryd.Execute
End Function
Sorry all, the above question is solved. There is some problem with the data itself, there are some non-numberic data in text_field. My apology. I try to run the SQL query in the SQL Server instead of Access, the SQL server clearly shows where is the cause of the problem. So, if you encounter TSQL issue in Access with unknown reason, you can try to execute the same query in SQL Server again and see if SQL server points out what exactly goes wrong. My aplogize for any inconvenience caused.
check your proper connection
ODBC DSN
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
"Dsn=DsnName;" +
"Uid=UserName;" +
"Pwd=Secret;";
conn.Open();
ODBC -- Standard Connection
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
"Driver={SQL Server};" +
"Server=DataBaseNamex;" +
"DataBase=DataBaseName;" +
"Uid=UserName;" +
"Pwd=Secret;";
conn.Open();
ODBC -- Trusted Connection
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString =
"Driver={SQL Server};" +
"Server=ServerName;" +
"DataBase=DataBaseName;" +
"Uid=;" +
"Pwd=;";
conn.Open();
// or
var conn = new OdbcConnection();
conn.ConnectionString =
"Driver={SQL Server};" +
"Server=ServerName;" +
"DataBase=DataBaseName;" +
"Trusted_Connection=Yes;";
conn.Open();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to write a SQL statement in a VB application. In this case, I need to declare two variables to call other stored procedures. This code works when I run it in SSMS, but I am not sure how to do it in VB.
Statement:
DECLARE #NewIncidentNoteId NVARCHAR(15)
EXEC uspGetNextId #NewIncidentNoteId OUTPUT
INSERT INTO [dbo].[IncidentNotes]
([Id]
,[IncidentId]
,[TaskId]
,[VLevel]
,[NotesSourceType]
,[Notes]
,[LogDate]
,[LogTime]
,[LogTZ]
,[LogBy]
,[LogByName]
,[NotifyFlg]
,[ActivityDate]
,[ActivityTime]
,[ShowInReport])
VALUES()
(#NewIncidentNoteId
,'60Z3M'
,''
,'1'
,'EIRInvestigatorComment'
,'This is a TEST note.'
,'20150416'
,'10:10:10'
,'UTC'
,'12876'
,'USER, ADMIN'
,'False'
,'20150416'
,'12:13:13'
,'1'))
First of all, as others have said, the best option would be to actually add it to the database as a stored procedure. Then you could just execute the stored procedure via a simple ADO.NET command. However, if you insist on sending the entire SQL script as a command, there are several ways to get it as a string.
VB doesn't currently have great support for multi-line string literals (long-overdue support for that is being added in VB.NET 14 as part of Visual Studio 2015). There are several workarounds. See this other answer of mine for some good options. However, for something like this, you may want to look into reading the script from an embedded resource or an external script file.
Set cmd = New ADODB.Command
cmd.ActiveConnection = con
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "empdetails"
cmd.Parameters.Append cmd.CreateParameter_
("empid", adVarChar, adParamInput, 6, str_empid)
Set rs = cmd.Execute
If Not rs.EOF Then
txt_firstname = rs.Fields(0)
txt_title = rs.Fields(1)
txt_address = rs.Fields(2)
End If
Set cmd.ActiveConnection = Nothing
this code show how to use SP in vb6 not sure which vb you use if it is VB.net then you can use ADO.net to do that
this link might be helpful too
http://www.codeproject.com/Articles/15222/How-to-Use-Stored-Procedures-in-VB
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to select table data in my page. Kindly provide a solution for my select query.
This is my code:
con.Open()
cmd = New SqlCommand("select name from wq where id='" + TextBox1.Text + "'", con)
dr = cmd.ExecuteReader
While dr.Read
TextBox2.Text = dr(0)
End While
dr.Close()
con.Close()
It looks like vb code if you want c# code then use following code
cmd = new SqlCommand("select name from wq where id='" + TextBox1.Text + "'", con);
con.Open();
dr = cmd.ExecuteReader();
dr.Read();
TextBox2.Text = dr[0].ToString();
dr.Close();
con.Close();